<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<title>Coverage for c:\program files\python26\lib\site-packages\modbus_tk-0.3-py2.6.egg\modbus_tk\modbus</title>
<link rel='stylesheet' href='style.css' type='text/css'>
<script type='text/javascript' src='jquery-1.3.2.min.js'></script>
<script type='text/javascript'>
function toggle_lines(btn, cls) {
    var btn = $(btn);
    var hide = "hide_"+cls;
    if (btn.hasClass(hide)) {
        $("#source ."+cls).removeClass(hide);
        btn.removeClass(hide);
    }
    else {
        $("#source ."+cls).addClass(hide);
        btn.addClass(hide);
    }
}
</script>
</head>
<body>
<div id='header'>
    <div class='content'>
        <h1>Coverage for <b>c:\program files\python26\lib\site-packages\modbus_tk-0.3-py2.6.egg\modbus_tk\modbus</b> :
            <span class='pc_cov'>83%</span>
        </h1>
        <h2 class='stats'>
            477 statements
            <span class='run hide_run' onclick='toggle_lines(this, "run")'>410 run</span>
            <span class='exc' onclick='toggle_lines(this, "exc")'>0 excluded</span>
            <span class='mis' onclick='toggle_lines(this, "mis")'>67 missing</span>
            
                <span class='par run hide_run' onclick='toggle_lines(this, "par")'>39 partial</span>
            
        </h2>
    </div>
</div>

<div id='source'>
<table cellspacing='0' cellpadding='0'>
<tr>
<td class='linenos' valign='top'>
<p class='pln'>1</p>
<p class='pln'>2</p>
<p class='stm run hide_run'>3</p>
<p class='pln'>4</p>
<p class='pln'>5</p>
<p class='pln'>6</p>
<p class='pln'>7</p>
<p class='pln'>8</p>
<p class='pln'>9</p>
<p class='pln'>10</p>
<p class='pln'>11</p>
<p class='pln'>12</p>
<p class='pln'>13</p>
<p class='pln'>14</p>
<p class='stm run hide_run'>15</p>
<p class='stm run hide_run'>16</p>
<p class='stm run hide_run'>17</p>
<p class='stm run hide_run'>18</p>
<p class='stm run hide_run'>19</p>
<p class='stm run hide_run'>20</p>
<p class='stm run hide_run'>21</p>
<p class='stm run hide_run'>22</p>
<p class='pln'>23</p>
<p class='pln'>24</p>
<p class='pln'>25</p>
<p class='stm run hide_run'>26</p>
<p class='pln'>27</p>
<p class='pln'>28</p>
<p class='pln'>29</p>
<p class='pln'>30</p>
<p class='pln'>31</p>
<p class='stm run hide_run'>32</p>
<p class='pln'>33</p>
<p class='pln'>34</p>
<p class='stm run hide_run'>35</p>
<p class='pln'>36</p>
<p class='stm par run hide_run'>37</p>
<p class='stm run hide_run'>38</p>
<p class='stm run hide_run'>39</p>
<p class='stm run hide_run'>40</p>
<p class='pln'>41</p>
<p class='stm run hide_run'>42</p>
<p class='pln'>43</p>
<p class='stm run hide_run'>44</p>
<p class='pln'>45</p>
<p class='pln'>46</p>
<p class='stm run hide_run'>47</p>
<p class='pln'>48</p>
<p class='pln'>49</p>
<p class='pln'>50</p>
<p class='stm run hide_run'>51</p>
<p class='pln'>52</p>
<p class='stm run hide_run'>53</p>
<p class='pln'>54</p>
<p class='pln'>55</p>
<p class='pln'>56</p>
<p class='pln'>57</p>
<p class='stm run hide_run'>58</p>
<p class='pln'>59</p>
<p class='stm run hide_run'>60</p>
<p class='pln'>61</p>
<p class='pln'>62</p>
<p class='pln'>63</p>
<p class='stm run hide_run'>64</p>
<p class='pln'>65</p>
<p class='stm run hide_run'>66</p>
<p class='pln'>67</p>
<p class='stm run hide_run'>68</p>
<p class='pln'>69</p>
<p class='stm run hide_run'>70</p>
<p class='pln'>71</p>
<p class='pln'>72</p>
<p class='pln'>73</p>
<p class='pln'>74</p>
<p class='stm run hide_run'>75</p>
<p class='pln'>76</p>
<p class='stm run hide_run'>77</p>
<p class='pln'>78</p>
<p class='pln'>79</p>
<p class='pln'>80</p>
<p class='pln'>81</p>
<p class='stm run hide_run'>82</p>
<p class='pln'>83</p>
<p class='stm run hide_run'>84</p>
<p class='pln'>85</p>
<p class='stm run hide_run'>86</p>
<p class='pln'>87</p>
<p class='stm run hide_run'>88</p>
<p class='pln'>89</p>
<p class='pln'>90</p>
<p class='pln'>91</p>
<p class='pln'>92</p>
<p class='stm run hide_run'>93</p>
<p class='pln'>94</p>
<p class='stm run hide_run'>95</p>
<p class='pln'>96</p>
<p class='pln'>97</p>
<p class='pln'>98</p>
<p class='pln'>99</p>
<p class='stm run hide_run'>100</p>
<p class='pln'>101</p>
<p class='pln'>102</p>
<p class='stm run hide_run'>103</p>
<p class='pln'>104</p>
<p class='pln'>105</p>
<p class='pln'>106</p>
<p class='pln'>107</p>
<p class='stm run hide_run'>108</p>
<p class='pln'>109</p>
<p class='stm run hide_run'>110</p>
<p class='pln'>111</p>
<p class='stm run hide_run'>112</p>
<p class='pln'>113</p>
<p class='pln'>114</p>
<p class='pln'>115</p>
<p class='pln'>116</p>
<p class='pln'>117</p>
<p class='stm mis'>118</p>
<p class='pln'>119</p>
<p class='stm run hide_run'>120</p>
<p class='pln'>121</p>
<p class='pln'>122</p>
<p class='pln'>123</p>
<p class='pln'>124</p>
<p class='pln'>125</p>
<p class='stm mis'>126</p>
<p class='pln'>127</p>
<p class='stm run hide_run'>128</p>
<p class='pln'>129</p>
<p class='pln'>130</p>
<p class='pln'>131</p>
<p class='pln'>132</p>
<p class='pln'>133</p>
<p class='stm mis'>134</p>
<p class='pln'>135</p>
<p class='stm run hide_run'>136</p>
<p class='pln'>137</p>
<p class='pln'>138</p>
<p class='pln'>139</p>
<p class='pln'>140</p>
<p class='pln'>141</p>
<p class='stm mis'>142</p>
<p class='pln'>143</p>
<p class='pln'>144</p>
<p class='stm run hide_run'>145</p>
<p class='pln'>146</p>
<p class='pln'>147</p>
<p class='pln'>148</p>
<p class='pln'>149</p>
<p class='pln'>150</p>
<p class='stm run hide_run'>151</p>
<p class='pln'>152</p>
<p class='stm run hide_run'>153</p>
<p class='stm run hide_run'>154</p>
<p class='stm run hide_run'>155</p>
<p class='pln'>156</p>
<p class='stm run hide_run'>157</p>
<p class='pln'>158</p>
<p class='stm run hide_run'>159</p>
<p class='pln'>160</p>
<p class='stm run hide_run'>161</p>
<p class='pln'>162</p>
<p class='stm mis'>163</p>
<p class='pln'>164</p>
<p class='stm run hide_run'>165</p>
<p class='pln'>166</p>
<p class='stm run hide_run'>167</p>
<p class='stm run hide_run'>168</p>
<p class='stm run hide_run'>169</p>
<p class='pln'>170</p>
<p class='stm run hide_run'>171</p>
<p class='pln'>172</p>
<p class='stm run hide_run'>173</p>
<p class='stm run hide_run'>174</p>
<p class='stm run hide_run'>175</p>
<p class='pln'>176</p>
<p class='stm run hide_run'>177</p>
<p class='pln'>178</p>
<p class='stm mis'>179</p>
<p class='pln'>180</p>
<p class='stm run hide_run'>181</p>
<p class='pln'>182</p>
<p class='stm mis'>183</p>
<p class='pln'>184</p>
<p class='stm run hide_run'>185</p>
<p class='pln'>186</p>
<p class='stm mis'>187</p>
<p class='pln'>188</p>
<p class='stm run hide_run'>189</p>
<p class='pln'>190</p>
<p class='pln'>191</p>
<p class='pln'>192</p>
<p class='pln'>193</p>
<p class='pln'>194</p>
<p class='stm mis'>195</p>
<p class='pln'>196</p>
<p class='stm run hide_run'>197</p>
<p class='pln'>198</p>
<p class='pln'>199</p>
<p class='pln'>200</p>
<p class='pln'>201</p>
<p class='stm mis'>202</p>
<p class='pln'>203</p>
<p class='stm run hide_run'>204</p>
<p class='stm run hide_run'>205</p>
<p class='pln'>206</p>
<p class='pln'>207</p>
<p class='pln'>208</p>
<p class='pln'>209</p>
<p class='pln'>210</p>
<p class='pln'>211</p>
<p class='pln'>212</p>
<p class='pln'>213</p>
<p class='pln'>214</p>
<p class='stm run hide_run'>215</p>
<p class='stm run hide_run'>216</p>
<p class='stm run hide_run'>217</p>
<p class='pln'>218</p>
<p class='pln'>219</p>
<p class='stm run hide_run'>220</p>
<p class='pln'>221</p>
<p class='pln'>222</p>
<p class='pln'>223</p>
<p class='stm run hide_run'>224</p>
<p class='stm run hide_run'>225</p>
<p class='stm run hide_run'>226</p>
<p class='stm run hide_run'>227</p>
<p class='stm run hide_run'>228</p>
<p class='stm run hide_run'>229</p>
<p class='stm run hide_run'>230</p>
<p class='stm par run hide_run'>231</p>
<p class='stm run hide_run'>232</p>
<p class='stm par run hide_run'>233</p>
<p class='stm run hide_run'>234</p>
<p class='pln'>235</p>
<p class='stm run hide_run'>236</p>
<p class='stm run hide_run'>237</p>
<p class='stm run hide_run'>238</p>
<p class='stm par run hide_run'>239</p>
<p class='stm run hide_run'>240</p>
<p class='stm par run hide_run'>241</p>
<p class='stm run hide_run'>242</p>
<p class='pln'>243</p>
<p class='stm run hide_run'>244</p>
<p class='stm run hide_run'>245</p>
<p class='stm run hide_run'>246</p>
<p class='stm run hide_run'>247</p>
<p class='stm run hide_run'>248</p>
<p class='stm par run hide_run'>249</p>
<p class='stm run hide_run'>250</p>
<p class='stm par run hide_run'>251</p>
<p class='stm run hide_run'>252</p>
<p class='pln'>253</p>
<p class='stm run hide_run'>254</p>
<p class='stm run hide_run'>255</p>
<p class='stm run hide_run'>256</p>
<p class='stm run hide_run'>257</p>
<p class='stm run hide_run'>258</p>
<p class='stm run hide_run'>259</p>
<p class='stm run hide_run'>260</p>
<p class='stm par run hide_run'>261</p>
<p class='stm run hide_run'>262</p>
<p class='stm run hide_run'>263</p>
<p class='stm run hide_run'>264</p>
<p class='stm run hide_run'>265</p>
<p class='pln'>266</p>
<p class='stm run hide_run'>267</p>
<p class='stm run hide_run'>268</p>
<p class='stm run hide_run'>269</p>
<p class='stm par run hide_run'>270</p>
<p class='stm run hide_run'>271</p>
<p class='stm par run hide_run'>272</p>
<p class='stm run hide_run'>273</p>
<p class='pln'>274</p>
<p class='stm run hide_run'>275</p>
<p class='stm run hide_run'>276</p>
<p class='stm run hide_run'>277</p>
<p class='stm run hide_run'>278</p>
<p class='stm run hide_run'>279</p>
<p class='stm par run hide_run'>280</p>
<p class='stm run hide_run'>281</p>
<p class='stm par run hide_run'>282</p>
<p class='stm run hide_run'>283</p>
<p class='pln'>284</p>
<p class='pln'>285</p>
<p class='stm run hide_run'>286</p>
<p class='pln'>287</p>
<p class='pln'>288</p>
<p class='stm run hide_run'>289</p>
<p class='pln'>290</p>
<p class='pln'>291</p>
<p class='stm run hide_run'>292</p>
<p class='pln'>293</p>
<p class='pln'>294</p>
<p class='stm run hide_run'>295</p>
<p class='stm par run hide_run'>296</p>
<p class='stm mis'>297</p>
<p class='stm par run hide_run'>298</p>
<p class='stm mis'>299</p>
<p class='stm run hide_run'>300</p>
<p class='pln'>301</p>
<p class='stm run hide_run'>302</p>
<p class='pln'>303</p>
<p class='stm run hide_run'>304</p>
<p class='pln'>305</p>
<p class='stm run hide_run'>306</p>
<p class='stm run hide_run'>307</p>
<p class='stm par run hide_run'>308</p>
<p class='stm mis'>309</p>
<p class='stm par run hide_run'>310</p>
<p class='stm mis'>311</p>
<p class='pln'>312</p>
<p class='pln'>313</p>
<p class='stm run hide_run'>314</p>
<p class='pln'>315</p>
<p class='pln'>316</p>
<p class='stm run hide_run'>317</p>
<p class='pln'>318</p>
<p class='stm run hide_run'>319</p>
<p class='pln'>320</p>
<p class='stm run hide_run'>321</p>
<p class='stm run hide_run'>322</p>
<p class='pln'>323</p>
<p class='stm run hide_run'>324</p>
<p class='pln'>325</p>
<p class='stm run hide_run'>326</p>
<p class='stm run hide_run'>327</p>
<p class='stm par run hide_run'>328</p>
<p class='pln'>329</p>
<p class='stm mis'>330</p>
<p class='pln'>331</p>
<p class='pln'>332</p>
<p class='pln'>333</p>
<p class='stm run hide_run'>334</p>
<p class='pln'>335</p>
<p class='pln'>336</p>
<p class='pln'>337</p>
<p class='stm run hide_run'>338</p>
<p class='stm run hide_run'>339</p>
<p class='stm run hide_run'>340</p>
<p class='stm run hide_run'>341</p>
<p class='stm run hide_run'>342</p>
<p class='stm run hide_run'>343</p>
<p class='stm run hide_run'>344</p>
<p class='stm run hide_run'>345</p>
<p class='stm run hide_run'>346</p>
<p class='stm run hide_run'>347</p>
<p class='stm run hide_run'>348</p>
<p class='pln'>349</p>
<p class='stm run hide_run'>350</p>
<p class='pln'>351</p>
<p class='stm run hide_run'>352</p>
<p class='pln'>353</p>
<p class='stm run hide_run'>354</p>
<p class='pln'>355</p>
<p class='stm run hide_run'>356</p>
<p class='pln'>357</p>
<p class='pln'>358</p>
<p class='stm run hide_run'>359</p>
<p class='pln'>360</p>
<p class='pln'>361</p>
<p class='stm run hide_run'>362</p>
<p class='pln'>363</p>
<p class='pln'>364</p>
<p class='pln'>365</p>
<p class='stm run hide_run'>366</p>
<p class='stm run hide_run'>367</p>
<p class='stm run hide_run'>368</p>
<p class='pln'>369</p>
<p class='stm run hide_run'>370</p>
<p class='pln'>371</p>
<p class='pln'>372</p>
<p class='pln'>373</p>
<p class='pln'>374</p>
<p class='stm par run hide_run'>375</p>
<p class='stm run hide_run'>376</p>
<p class='stm mis'>377</p>
<p class='stm mis'>378</p>
<p class='stm mis'>379</p>
<p class='pln'>380</p>
<p class='pln'>381</p>
<p class='stm run hide_run'>382</p>
<p class='pln'>383</p>
<p class='pln'>384</p>
<p class='pln'>385</p>
<p class='pln'>386</p>
<p class='pln'>387</p>
<p class='stm run hide_run'>388</p>
<p class='pln'>389</p>
<p class='stm run hide_run'>390</p>
<p class='stm run hide_run'>391</p>
<p class='pln'>392</p>
<p class='stm run hide_run'>393</p>
<p class='pln'>394</p>
<p class='stm run hide_run'>395</p>
<p class='pln'>396</p>
<p class='stm run hide_run'>397</p>
<p class='pln'>398</p>
<p class='pln'>399</p>
<p class='pln'>400</p>
<p class='pln'>401</p>
<p class='pln'>402</p>
<p class='pln'>403</p>
<p class='pln'>404</p>
<p class='pln'>405</p>
<p class='pln'>406</p>
<p class='stm run hide_run'>407</p>
<p class='pln'>408</p>
<p class='stm run hide_run'>409</p>
<p class='stm run hide_run'>410</p>
<p class='stm run hide_run'>411</p>
<p class='stm run hide_run'>412</p>
<p class='stm run hide_run'>413</p>
<p class='pln'>414</p>
<p class='stm run hide_run'>415</p>
<p class='pln'>416</p>
<p class='stm run hide_run'>417</p>
<p class='pln'>418</p>
<p class='stm run hide_run'>419</p>
<p class='pln'>420</p>
<p class='stm run hide_run'>421</p>
<p class='pln'>422</p>
<p class='stm run hide_run'>423</p>
<p class='pln'>424</p>
<p class='stm run hide_run'>425</p>
<p class='pln'>426</p>
<p class='stm run hide_run'>427</p>
<p class='pln'>428</p>
<p class='pln'>429</p>
<p class='stm run hide_run'>430</p>
<p class='stm run hide_run'>431</p>
<p class='stm run hide_run'>432</p>
<p class='pln'>433</p>
<p class='pln'>434</p>
<p class='stm run hide_run'>435</p>
<p class='pln'>436</p>
<p class='stm run hide_run'>437</p>
<p class='stm run hide_run'>438</p>
<p class='stm run hide_run'>439</p>
<p class='stm run hide_run'>440</p>
<p class='stm run hide_run'>441</p>
<p class='pln'>442</p>
<p class='stm run hide_run'>443</p>
<p class='pln'>444</p>
<p class='stm run hide_run'>445</p>
<p class='pln'>446</p>
<p class='stm run hide_run'>447</p>
<p class='pln'>448</p>
<p class='pln'>449</p>
<p class='stm run hide_run'>450</p>
<p class='stm run hide_run'>451</p>
<p class='stm run hide_run'>452</p>
<p class='pln'>453</p>
<p class='stm run hide_run'>454</p>
<p class='pln'>455</p>
<p class='stm run hide_run'>456</p>
<p class='pln'>457</p>
<p class='stm run hide_run'>458</p>
<p class='pln'>459</p>
<p class='stm run hide_run'>460</p>
<p class='pln'>461</p>
<p class='stm run hide_run'>462</p>
<p class='pln'>463</p>
<p class='stm run hide_run'>464</p>
<p class='pln'>465</p>
<p class='stm run hide_run'>466</p>
<p class='pln'>467</p>
<p class='stm run hide_run'>468</p>
<p class='stm run hide_run'>469</p>
<p class='pln'>470</p>
<p class='pln'>471</p>
<p class='stm run hide_run'>472</p>
<p class='pln'>473</p>
<p class='pln'>474</p>
<p class='stm run hide_run'>475</p>
<p class='pln'>476</p>
<p class='pln'>477</p>
<p class='stm run hide_run'>478</p>
<p class='pln'>479</p>
<p class='stm run hide_run'>480</p>
<p class='stm run hide_run'>481</p>
<p class='stm run hide_run'>482</p>
<p class='pln'>483</p>
<p class='stm run hide_run'>484</p>
<p class='pln'>485</p>
<p class='stm run hide_run'>486</p>
<p class='pln'>487</p>
<p class='stm run hide_run'>488</p>
<p class='pln'>489</p>
<p class='stm run hide_run'>490</p>
<p class='pln'>491</p>
<p class='stm run hide_run'>492</p>
<p class='pln'>493</p>
<p class='pln'>494</p>
<p class='stm run hide_run'>495</p>
<p class='pln'>496</p>
<p class='stm run hide_run'>497</p>
<p class='pln'>498</p>
<p class='stm run hide_run'>499</p>
<p class='pln'>500</p>
<p class='pln'>501</p>
<p class='stm run hide_run'>502</p>
<p class='pln'>503</p>
<p class='stm run hide_run'>504</p>
<p class='stm run hide_run'>505</p>
<p class='stm run hide_run'>506</p>
<p class='stm run hide_run'>507</p>
<p class='pln'>508</p>
<p class='stm run hide_run'>509</p>
<p class='pln'>510</p>
<p class='stm run hide_run'>511</p>
<p class='pln'>512</p>
<p class='pln'>513</p>
<p class='stm run hide_run'>514</p>
<p class='pln'>515</p>
<p class='stm run hide_run'>516</p>
<p class='stm run hide_run'>517</p>
<p class='stm run hide_run'>518</p>
<p class='pln'>519</p>
<p class='stm run hide_run'>520</p>
<p class='pln'>521</p>
<p class='stm run hide_run'>522</p>
<p class='pln'>523</p>
<p class='pln'>524</p>
<p class='stm run hide_run'>525</p>
<p class='pln'>526</p>
<p class='stm run hide_run'>527</p>
<p class='stm run hide_run'>528</p>
<p class='stm par run hide_run'>529</p>
<p class='stm mis'>530</p>
<p class='stm run hide_run'>531</p>
<p class='stm run hide_run'>532</p>
<p class='stm run hide_run'>533</p>
<p class='stm run hide_run'>534</p>
<p class='pln'>535</p>
<p class='stm run hide_run'>536</p>
<p class='stm run hide_run'>537</p>
<p class='stm run hide_run'>538</p>
<p class='stm run hide_run'>539</p>
<p class='stm run hide_run'>540</p>
<p class='pln'>541</p>
<p class='stm run hide_run'>542</p>
<p class='pln'>543</p>
<p class='stm run hide_run'>544</p>
<p class='stm run hide_run'>545</p>
<p class='stm run hide_run'>546</p>
<p class='stm run hide_run'>547</p>
<p class='pln'>548</p>
<p class='stm run hide_run'>549</p>
<p class='pln'>550</p>
<p class='stm run hide_run'>551</p>
<p class='stm run hide_run'>552</p>
<p class='stm run hide_run'>553</p>
<p class='stm run hide_run'>554</p>
<p class='stm run hide_run'>555</p>
<p class='stm run hide_run'>556</p>
<p class='pln'>557</p>
<p class='stm run hide_run'>558</p>
<p class='stm run hide_run'>559</p>
<p class='pln'>560</p>
<p class='stm run hide_run'>561</p>
<p class='pln'>562</p>
<p class='pln'>563</p>
<p class='pln'>564</p>
<p class='pln'>565</p>
<p class='stm run hide_run'>566</p>
<p class='stm run hide_run'>567</p>
<p class='stm run hide_run'>568</p>
<p class='stm par run hide_run'>569</p>
<p class='stm mis'>570</p>
<p class='pln'>571</p>
<p class='pln'>572</p>
<p class='stm run hide_run'>573</p>
<p class='pln'>574</p>
<p class='pln'>575</p>
<p class='stm par run hide_run'>576</p>
<p class='stm mis'>577</p>
<p class='pln'>578</p>
<p class='pln'>579</p>
<p class='stm run hide_run'>580</p>
<p class='pln'>581</p>
<p class='stm run hide_run'>582</p>
<p class='stm run hide_run'>583</p>
<p class='pln'>584</p>
<p class='pln'>585</p>
<p class='stm run hide_run'>586</p>
<p class='stm par run hide_run'>587</p>
<p class='stm run hide_run'>588</p>
<p class='stm run hide_run'>589</p>
<p class='stm run hide_run'>590</p>
<p class='stm run hide_run'>591</p>
<p class='pln'>592</p>
<p class='stm run hide_run'>593</p>
<p class='stm mis'>594</p>
<p class='pln'>595</p>
<p class='stm run hide_run'>596</p>
<p class='stm run hide_run'>597</p>
<p class='stm run hide_run'>598</p>
<p class='stm run hide_run'>599</p>
<p class='pln'>600</p>
<p class='pln'>601</p>
<p class='stm run hide_run'>602</p>
<p class='pln'>603</p>
<p class='stm run hide_run'>604</p>
<p class='stm par run hide_run'>605</p>
<p class='stm mis'>606</p>
<p class='stm par run hide_run'>607</p>
<p class='stm mis'>608</p>
<p class='stm par run hide_run'>609</p>
<p class='stm mis'>610</p>
<p class='pln'>611</p>
<p class='stm par run hide_run'>612</p>
<p class='stm mis'>613</p>
<p class='pln'>614</p>
<p class='pln'>615</p>
<p class='pln'>616</p>
<p class='pln'>617</p>
<p class='stm run hide_run'>618</p>
<p class='stm run hide_run'>619</p>
<p class='stm run hide_run'>620</p>
<p class='stm par run hide_run'>621</p>
<p class='stm mis'>622</p>
<p class='stm par run hide_run'>623</p>
<p class='stm mis'>624</p>
<p class='stm mis'>625</p>
<p class='pln'>626</p>
<p class='pln'>627</p>
<p class='stm run hide_run'>628</p>
<p class='pln'>629</p>
<p class='stm run hide_run'>630</p>
<p class='pln'>631</p>
<p class='stm run hide_run'>632</p>
<p class='pln'>633</p>
<p class='pln'>634</p>
<p class='pln'>635</p>
<p class='pln'>636</p>
<p class='stm mis'>637</p>
<p class='stm mis'>638</p>
<p class='pln'>639</p>
<p class='pln'>640</p>
<p class='stm mis'>641</p>
<p class='stm mis'>642</p>
<p class='pln'>643</p>
<p class='stm run hide_run'>644</p>
<p class='pln'>645</p>
<p class='pln'>646</p>
<p class='pln'>647</p>
<p class='stm mis'>648</p>
<p class='stm mis'>649</p>
<p class='stm mis'>650</p>
<p class='stm mis'>651</p>
<p class='pln'>652</p>
<p class='stm run hide_run'>653</p>
<p class='pln'>654</p>
<p class='stm par run hide_run'>655</p>
<p class='stm mis'>656</p>
<p class='stm run hide_run'>657</p>
<p class='stm par run hide_run'>658</p>
<p class='stm run hide_run'>659</p>
<p class='stm run hide_run'>660</p>
<p class='stm mis'>661</p>
<p class='pln'>662</p>
<p class='stm run hide_run'>663</p>
<p class='pln'>664</p>
<p class='pln'>665</p>
<p class='pln'>666</p>
<p class='pln'>667</p>
<p class='pln'>668</p>
<p class='stm run hide_run'>669</p>
<p class='stm run hide_run'>670</p>
<p class='pln'>671</p>
<p class='pln'>672</p>
<p class='pln'>673</p>
<p class='stm run hide_run'>674</p>
<p class='pln'>675</p>
<p class='stm run hide_run'>676</p>
<p class='stm run hide_run'>677</p>
<p class='stm run hide_run'>678</p>
<p class='pln'>679</p>
<p class='stm par run hide_run'>680</p>
<p class='stm mis'>681</p>
<p class='pln'>682</p>
<p class='pln'>683</p>
<p class='stm run hide_run'>684</p>
<p class='stm run hide_run'>685</p>
<p class='pln'>686</p>
<p class='stm run hide_run'>687</p>
<p class='pln'>688</p>
<p class='stm run hide_run'>689</p>
<p class='pln'>690</p>
<p class='pln'>691</p>
<p class='pln'>692</p>
<p class='stm run hide_run'>693</p>
<p class='stm run hide_run'>694</p>
<p class='pln'>695</p>
<p class='pln'>696</p>
<p class='pln'>697</p>
<p class='stm run hide_run'>698</p>
<p class='pln'>699</p>
<p class='stm par run hide_run'>700</p>
<p class='stm mis'>701</p>
<p class='pln'>702</p>
<p class='pln'>703</p>
<p class='stm run hide_run'>704</p>
<p class='stm run hide_run'>705</p>
<p class='pln'>706</p>
<p class='stm run hide_run'>707</p>
<p class='pln'>708</p>
<p class='stm run hide_run'>709</p>
<p class='pln'>710</p>
<p class='pln'>711</p>
<p class='stm run hide_run'>712</p>
<p class='pln'>713</p>
<p class='stm run hide_run'>714</p>
<p class='stm run hide_run'>715</p>
<p class='pln'>716</p>
<p class='stm run hide_run'>717</p>
<p class='pln'>718</p>
<p class='stm run hide_run'>719</p>
<p class='stm par run hide_run'>720</p>
<p class='stm mis'>721</p>
<p class='stm par run hide_run'>722</p>
<p class='stm run hide_run'>723</p>
<p class='stm run hide_run'>724</p>
<p class='pln'>725</p>
<p class='stm mis'>726</p>
<p class='pln'>727</p>
<p class='stm run hide_run'>728</p>
<p class='pln'>729</p>
<p class='stm run hide_run'>730</p>
<p class='stm par run hide_run'>731</p>
<p class='stm run hide_run'>732</p>
<p class='pln'>733</p>
<p class='stm mis'>734</p>
<p class='pln'>735</p>
<p class='stm run hide_run'>736</p>
<p class='pln'>737</p>
<p class='stm mis'>738</p>
<p class='stm mis'>739</p>
<p class='stm mis'>740</p>
<p class='pln'>741</p>
<p class='stm mis'>742</p>
<p class='pln'>743</p>
<p class='stm run hide_run'>744</p>
<p class='pln'>745</p>
<p class='stm mis'>746</p>
<p class='stm mis'>747</p>
<p class='pln'>748</p>
<p class='stm run hide_run'>749</p>
<p class='pln'>750</p>
<p class='pln'>751</p>
<p class='pln'>752</p>
<p class='stm run hide_run'>753</p>
<p class='stm run hide_run'>754</p>
<p class='pln'>755</p>
<p class='stm run hide_run'>756</p>
<p class='pln'>757</p>
<p class='pln'>758</p>
<p class='stm run hide_run'>759</p>
<p class='pln'>760</p>
<p class='stm run hide_run'>761</p>
<p class='stm run hide_run'>762</p>
<p class='stm run hide_run'>763</p>
<p class='pln'>764</p>
<p class='stm run hide_run'>765</p>
<p class='stm run hide_run'>766</p>
<p class='pln'>767</p>
<p class='stm run hide_run'>768</p>
<p class='stm run hide_run'>769</p>
<p class='stm run hide_run'>770</p>
<p class='stm run hide_run'>771</p>
<p class='stm run hide_run'>772</p>
<p class='stm mis'>773</p>
<p class='stm mis'>774</p>
<p class='pln'>775</p>
<p class='pln'>776</p>
<p class='stm run hide_run'>777</p>
<p class='stm run hide_run'>778</p>
<p class='stm run hide_run'>779</p>
<p class='stm run hide_run'>780</p>
<p class='pln'>781</p>
<p class='pln'>782</p>
<p class='stm run hide_run'>783</p>
<p class='pln'>784</p>
<p class='pln'>785</p>
<p class='pln'>786</p>
<p class='pln'>787</p>
<p class='pln'>788</p>
<p class='stm run hide_run'>789</p>
<p class='pln'>790</p>
<p class='stm run hide_run'>791</p>
<p class='stm run hide_run'>792</p>
<p class='stm run hide_run'>793</p>
<p class='stm run hide_run'>794</p>
<p class='stm run hide_run'>795</p>
<p class='pln'>796</p>
<p class='stm run hide_run'>797</p>
<p class='pln'>798</p>
<p class='stm mis'>799</p>
<p class='pln'>800</p>
<p class='stm run hide_run'>801</p>
<p class='pln'>802</p>
<p class='stm mis'>803</p>
<p class='pln'>804</p>
<p class='stm run hide_run'>805</p>
<p class='pln'>806</p>
<p class='stm mis'>807</p>
<p class='pln'>808</p>
<p class='stm run hide_run'>809</p>
<p class='pln'>810</p>
<p class='stm run hide_run'>811</p>
<p class='stm run hide_run'>812</p>
<p class='pln'>813</p>
<p class='stm run hide_run'>814</p>
<p class='pln'>815</p>
<p class='stm mis'>816</p>
<p class='pln'>817</p>
<p class='stm run hide_run'>818</p>
<p class='pln'>819</p>
<p class='stm mis'>820</p>
<p class='pln'>821</p>
<p class='stm run hide_run'>822</p>
<p class='pln'>823</p>
<p class='stm run hide_run'>824</p>
<p class='pln'>825</p>
<p class='stm run hide_run'>826</p>
<p class='pln'>827</p>
<p class='stm mis'>828</p>
<p class='pln'>829</p>
<p class='stm run hide_run'>830</p>
<p class='pln'>831</p>
<p class='stm mis'>832</p>
<p class='pln'>833</p>
<p class='stm run hide_run'>834</p>
<p class='pln'>835</p>
<p class='stm mis'>836</p>
<p class='pln'>837</p>
<p class='stm run hide_run'>838</p>
<p class='pln'>839</p>
<p class='pln'>840</p>
<p class='pln'>841</p>
<p class='pln'>842</p>
<p class='stm mis'>843</p>
<p class='pln'>844</p>
<p class='stm run hide_run'>845</p>
<p class='pln'>846</p>
<p class='stm run hide_run'>847</p>
<p class='stm run hide_run'>848</p>
<p class='pln'>849</p>
<p class='stm run hide_run'>850</p>
<p class='pln'>851</p>
<p class='stm run hide_run'>852</p>
<p class='stm run hide_run'>853</p>
<p class='stm run hide_run'>854</p>
<p class='pln'>855</p>
<p class='stm run hide_run'>856</p>
<p class='pln'>857</p>
<p class='stm run hide_run'>858</p>
<p class='stm run hide_run'>859</p>
<p class='stm run hide_run'>860</p>
<p class='stm run hide_run'>861</p>
<p class='stm run hide_run'>862</p>
<p class='stm run hide_run'>863</p>
<p class='stm mis'>864</p>
<p class='stm mis'>865</p>
<p class='pln'>866</p>
<p class='stm run hide_run'>867</p>
<p class='pln'>868</p>
<p class='pln'>869</p>
<p class='stm par run hide_run'>870</p>
<p class='stm mis'>871</p>
<p class='pln'>872</p>
<p class='pln'>873</p>
<p class='stm run hide_run'>874</p>
<p class='pln'>875</p>
<p class='stm run hide_run'>876</p>
<p class='stm par run hide_run'>877</p>
<p class='stm mis'>878</p>
<p class='pln'>879</p>
<p class='stm run hide_run'>880</p>
<p class='stm run hide_run'>881</p>
<p class='stm par run hide_run'>882</p>
<p class='stm mis'>883</p>
<p class='pln'>884</p>
<p class='stm par run hide_run'>885</p>
<p class='stm mis'>886</p>
<p class='stm run hide_run'>887</p>
<p class='pln'>888</p>
    
</td>
<td class='text' valign='top'>
<p class='pln'><span class='com'>#!/usr/bin/env python</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='com'># -*- coding: utf-8 -*-</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'> Modbus TestKit: Implementation of Modbus protocol in python</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'></span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'> (C)2009 - Luc Jean - luc.jean@gmail.com</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'> (C)2009 - Apidev - http://www.apidev.fr</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'></span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'> This is distributed under GNU LGPL license, see license.txt</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'></span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'> History:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'> 2010/01/08 - RD: Update master.execute(..) to calculate lengths automatically based on requested command</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>from</span> <span class='nam'>__future__</span> <span class='key'>import</span> <span class='nam'>with_statement</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>import</span> <span class='nam'>struct</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>import</span> <span class='nam'>logging</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>import</span> <span class='nam'>defines</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>import</span> <span class='nam'>threading</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>import</span> <span class='nam'>utils</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>from</span> <span class='nam'>utils</span> <span class='key'>import</span> <span class='nam'>threadsafe_function</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>from</span> <span class='nam'>hooks</span> <span class='key'>import</span> <span class='nam'>call_hooks</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='com'>#modbus_tk is using the python logging mechanism</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='com'>#you can define this logger in your app in order to see its prints logs</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='nam'>LOGGER</span> <span class='op'>=</span> <span class='nam'>logging</span><span class='op'>.</span><span class='nam'>getLogger</span><span class='op'>(</span><span class='str'>&quot;modbus_tk&quot;</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='com'>#-------------------------------------------------------------------------------</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='com'>#Exceptions</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='com'>#-------------------------------------------------------------------------------</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>class</span> <span class='nam'>ModbusError</span><span class='op'>(</span><span class='nam'>Exception</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Exception raised when the modbus slave returns an error&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>__init__</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>exception_code</span><span class='op'>,</span> <span class='nam'>value</span><span class='op'>=</span><span class='str'>&quot;&quot;</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;constructor: set the exception code returned by the slave&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>39</span>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='key'>not</span> <span class='nam'>value</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>value</span> <span class='op'>=</span> <span class='str'>&quot;Modbus Error: Exception code = %d&quot;</span> <span class='op'>%</span> <span class='op'>(</span><span class='nam'>exception_code</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>Exception</span><span class='op'>.</span><span class='nam'>__init__</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>value</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_exception_code</span> <span class='op'>=</span> <span class='nam'>exception_code</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>get_exception_code</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;return the exception code returned by the slave (see defines)&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_exception_code</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='com'>#-------------------------------------------------------------------------------</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>class</span> <span class='nam'>ModbusFunctionNotSupportedError</span><span class='op'>(</span><span class='nam'>Exception</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; Exception raised when calling a modbus function not supported by modbus_tk</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>pass</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>class</span> <span class='nam'>DuplicatedKeyError</span><span class='op'>(</span><span class='nam'>Exception</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; Exception raised when trying to add an object with a key that is already </span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; used for another object</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>pass</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>class</span> <span class='nam'>MissingKeyError</span><span class='op'>(</span><span class='nam'>Exception</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; Exception raised when trying to get an object with a key that doesn&#39;t exist</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>pass</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>class</span> <span class='nam'>InvalidModbusBlockError</span><span class='op'>(</span><span class='nam'>Exception</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Exception raised when a modbus block is not valid&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>pass</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>class</span> <span class='nam'>InvalidArgumentError</span><span class='op'>(</span><span class='nam'>Exception</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; Exception raised when one argument of a function doesn&#39;t meet </span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; what is expected</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>pass</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>class</span> <span class='nam'>OverlapModbusBlockError</span><span class='op'>(</span><span class='nam'>Exception</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; Exception raised when adding modbus block on a memory address</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; range already in use</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>pass</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>class</span> <span class='nam'>OutOfModbusBlockError</span><span class='op'>(</span><span class='nam'>Exception</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Exception raised when accessing out of a modbus block&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>pass</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>class</span> <span class='nam'>ModbusInvalidResponseError</span><span class='op'>(</span><span class='nam'>Exception</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; Exception raised when the response sent by the slave doesn&#39;t fit </span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; with the expected format</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>pass</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>class</span> <span class='nam'>ModbusInvalidRequestError</span><span class='op'>(</span><span class='nam'>Exception</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; Exception raised when the request by the master doesn&#39;t fit </span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; with the expected format</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>pass</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='com'>#-------------------------------------------------------------------------------</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>class</span> <span class='nam'>Query</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; Interface to be implemented in subclass for every specific modbus MAC layer</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>__init__</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Constructor&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>pass</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>build_request</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>pdu</span><span class='op'>,</span> <span class='nam'>slave</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; Get the modbus application protocol request pdu and slave id</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; Encapsulate with MAC layer information</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; Returns a string</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>NotImplementedError</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>parse_response</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>response</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; Get the full response and extract the modbus application protocol</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; response pdu</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; Returns a string</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>NotImplementedError</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>parse_request</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>request</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; Get the full request and extract the modbus application protocol </span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; request pdu</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; Returns a string and the slave id</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>NotImplementedError</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>build_response</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>response_pdu</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; Get the modbus application protocol response pdu and encapsulate with</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; MAC layer information</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; Returns a string</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>NotImplementedError</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='com'>#-------------------------------------------------------------------------------</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>class</span> <span class='nam'>Master</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; This class implements the Modbus Application protocol for a master</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; To be subclassed with a class implementing the MAC layer</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>__init__</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>timeout_in_sec</span><span class='op'>,</span> <span class='nam'>hooks</span><span class='op'>=</span><span class='nam'>None</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Constructor: can define a timeout&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_timeout</span> <span class='op'>=</span> <span class='nam'>timeout_in_sec</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_verbose</span> <span class='op'>=</span> <span class='nam'>False</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_is_opened</span> <span class='op'>=</span> <span class='nam'>False</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>__del__</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Destructor: close the connection&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>close</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>set_verbose</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>verbose</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;print some more log prints for debug purpose&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_verbose</span> <span class='op'>=</span> <span class='nam'>verbose</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>open</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;open the communication with the slave&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='key'>not</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_is_opened</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_do_open</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_is_opened</span> <span class='op'>=</span> <span class='nam'>True</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>close</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;close the communication with the slave&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_is_opened</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_do_close</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_is_opened</span> <span class='op'>=</span> <span class='nam'>False</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_do_open</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Open the MAC layer&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>NotImplementedError</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_do_close</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Close the MAC layer&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>NotImplementedError</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_send</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>buf</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Send data to a slave on the MAC layer&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>NotImplementedError</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_recv</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>expected_length</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; Receive data from a slave on the MAC layer</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; if expected_length is &gt;=0 then consider that the response is done when this</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; number of bytes is received</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>NotImplementedError</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_make_query</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; Returns an instance of a Query subclass implementing </span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; the MAC layer protocol</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>NotImplementedError</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='op'>@</span><span class='nam'>threadsafe_function</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>execute</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>slave</span><span class='op'>,</span> <span class='nam'>function_code</span><span class='op'>,</span> <span class='nam'>starting_address</span><span class='op'>,</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>quantity_of_x</span><span class='op'>=</span><span class='num'>0</span><span class='op'>,</span> <span class='nam'>output_value</span><span class='op'>=</span><span class='num'>0</span><span class='op'>,</span> <span class='nam'>data_format</span><span class='op'>=</span><span class='str'>&quot;&quot;</span><span class='op'>,</span> <span class='nam'>expected_length</span><span class='op'>=</span><span class='op'>-</span><span class='num'>1</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; Execute a modbus query and returns the data part of the answer as a tuple</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; The returned tuple depends on the query function code. see modbus protocol</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; specification for details</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; data_format makes possible to extract the data like defined in the</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; struct python module documentation</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>pdu</span> <span class='op'>=</span> <span class='str'>&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>is_read_function</span> <span class='op'>=</span> <span class='nam'>False</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>nb_of_digits</span> <span class='op'>=</span> <span class='num'>0</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='com'>#open the connection if it is not already done</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>open</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='com'>#Build the modbus pdu and the format of the expected data.</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='com'>#It depends of function code. see modbus specifications for details.</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>function_code</span> <span class='op'>==</span> <span class='nam'>defines</span><span class='op'>.</span><span class='nam'>READ_COILS</span> <span class='key'>or</span> <span class='nam'>function_code</span> <span class='op'>==</span> <span class='nam'>defines</span><span class='op'>.</span><span class='nam'>READ_DISCRETE_INPUTS</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>is_read_function</span> <span class='op'>=</span> <span class='nam'>True</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>pdu</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;BHH&quot;</span><span class='op'>,</span> <span class='nam'>function_code</span><span class='op'>,</span> <span class='nam'>starting_address</span><span class='op'>,</span> <span class='nam'>quantity_of_x</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>byte_count</span> <span class='op'>=</span> <span class='nam'>quantity_of_x</span> <span class='op'>/</span> <span class='num'>8</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='op'>(</span><span class='nam'>quantity_of_x</span> <span class='op'>%</span> <span class='num'>8</span><span class='op'>)</span> <span class='op'>&gt;</span> <span class='num'>0</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>byte_count</span> <span class='op'>+=</span> <span class='num'>1</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>nb_of_digits</span> <span class='op'>=</span> <span class='nam'>quantity_of_x</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>233</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='key'>not</span> <span class='nam'>data_format</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>data_format</span> <span class='op'>=</span> <span class='str'>&quot;&gt;&quot;</span><span class='op'>+</span><span class='op'>(</span><span class='nam'>byte_count</span><span class='op'>*</span><span class='str'>&quot;B&quot;</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>289</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>expected_length</span> <span class='op'>&lt;</span> <span class='num'>0</span><span class='op'>:</span>&nbsp; <span class='com'>#No lenght was specified and calculated length can be used:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>expected_length</span> <span class='op'>=</span> <span class='nam'>byte_count</span> <span class='op'>+</span> <span class='num'>5</span>&nbsp; <span class='com'>#slave + func + bytcodeLen + bytecode + crc1 + crc2</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>elif</span> <span class='nam'>function_code</span> <span class='op'>==</span> <span class='nam'>defines</span><span class='op'>.</span><span class='nam'>READ_INPUT_REGISTERS</span> <span class='key'>or</span> <span class='nam'>function_code</span> <span class='op'>==</span> <span class='nam'>defines</span><span class='op'>.</span><span class='nam'>READ_HOLDING_REGISTERS</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>is_read_function</span> <span class='op'>=</span> <span class='nam'>True</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>pdu</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;BHH&quot;</span><span class='op'>,</span> <span class='nam'>function_code</span><span class='op'>,</span> <span class='nam'>starting_address</span><span class='op'>,</span> <span class='nam'>quantity_of_x</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>241</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='key'>not</span> <span class='nam'>data_format</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>data_format</span> <span class='op'>=</span> <span class='str'>&quot;&gt;&quot;</span><span class='op'>+</span><span class='op'>(</span><span class='nam'>quantity_of_x</span><span class='op'>*</span><span class='str'>&quot;H&quot;</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>289</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>expected_length</span> <span class='op'>&lt;</span> <span class='num'>0</span><span class='op'>:</span>&nbsp; <span class='com'>#No lenght was specified and calculated length can be used:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>expected_length</span> <span class='op'>=</span> <span class='num'>2</span><span class='op'>*</span><span class='nam'>quantity_of_x</span> <span class='op'>+</span> <span class='num'>5</span>&nbsp; <span class='com'>#slave + func + bytcodeLen + bytecode x 2 + crc1 + crc2</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>elif</span> <span class='op'>(</span><span class='nam'>function_code</span> <span class='op'>==</span> <span class='nam'>defines</span><span class='op'>.</span><span class='nam'>WRITE_SINGLE_COIL</span><span class='op'>)</span> <span class='key'>or</span> <span class='op'>(</span><span class='nam'>function_code</span> <span class='op'>==</span> <span class='nam'>defines</span><span class='op'>.</span><span class='nam'>WRITE_SINGLE_REGISTER</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>function_code</span> <span class='op'>==</span> <span class='nam'>defines</span><span class='op'>.</span><span class='nam'>WRITE_SINGLE_COIL</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>output_value</span> <span class='op'>!=</span> <span class='num'>0</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>output_value</span> <span class='op'>=</span> <span class='num'>0xff00</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>pdu</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;BHH&quot;</span><span class='op'>,</span> <span class='nam'>function_code</span><span class='op'>,</span> <span class='nam'>starting_address</span><span class='op'>,</span> <span class='nam'>output_value</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>251</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='key'>not</span> <span class='nam'>data_format</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>data_format</span> <span class='op'>=</span> <span class='str'>&quot;&gt;HH&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>289</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>expected_length</span> <span class='op'>&lt;</span> <span class='num'>0</span><span class='op'>:</span>&nbsp; <span class='com'>#No lenght was specified and calculated length can be used:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>expected_length</span> <span class='op'>=</span> <span class='num'>8</span>&nbsp; <span class='com'>#slave + func + adress1 + adress2 + value1+value2 + crc1 + crc2</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>elif</span> <span class='nam'>function_code</span> <span class='op'>==</span> <span class='nam'>defines</span><span class='op'>.</span><span class='nam'>WRITE_MULTIPLE_COILS</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>byte_count</span> <span class='op'>=</span> <span class='nam'>len</span><span class='op'>(</span><span class='nam'>output_value</span><span class='op'>)</span> <span class='op'>/</span> <span class='num'>8</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='op'>(</span><span class='nam'>len</span><span class='op'>(</span><span class='nam'>output_value</span><span class='op'>)</span> <span class='op'>%</span> <span class='num'>8</span><span class='op'>)</span> <span class='op'>&gt;</span> <span class='num'>0</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>byte_count</span> <span class='op'>+=</span> <span class='num'>1</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>pdu</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;BHHB&quot;</span><span class='op'>,</span> <span class='nam'>function_code</span><span class='op'>,</span> <span class='nam'>starting_address</span><span class='op'>,</span> <span class='nam'>len</span><span class='op'>(</span><span class='nam'>output_value</span><span class='op'>)</span><span class='op'>,</span> <span class='nam'>byte_count</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>i</span><span class='op'>,</span> <span class='nam'>byte_value</span> <span class='op'>=</span> <span class='num'>0</span><span class='op'>,</span> <span class='num'>0</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>j</span> <span class='key'>in</span> <span class='nam'>output_value</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>263</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>j</span> <span class='op'>&gt;</span> <span class='num'>0</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>byte_value</span> <span class='op'>+=</span> <span class='nam'>pow</span><span class='op'>(</span><span class='num'>2</span><span class='op'>,</span> <span class='nam'>i</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>i</span> <span class='op'>==</span> <span class='num'>7</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>pdu</span> <span class='op'>+=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;B&quot;</span><span class='op'>,</span> <span class='nam'>byte_value</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>i</span><span class='op'>,</span> <span class='nam'>byte_value</span> <span class='op'>=</span> <span class='num'>0</span><span class='op'>,</span> <span class='num'>0</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>else</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>i</span> <span class='op'>+=</span> <span class='num'>1</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>i</span> <span class='op'>&gt;</span> <span class='num'>0</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>pdu</span> <span class='op'>+=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;B&quot;</span><span class='op'>,</span> <span class='nam'>byte_value</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>272</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='key'>not</span> <span class='nam'>data_format</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>data_format</span> <span class='op'>=</span> <span class='str'>&quot;&gt;HH&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>289</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>expected_length</span> <span class='op'>&lt;</span> <span class='num'>0</span><span class='op'>:</span>&nbsp; <span class='com'>#No lenght was specified and calculated length can be used:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>expected_length</span> <span class='op'>=</span> <span class='num'>8</span>&nbsp; <span class='com'>#slave + func + adress1 + adress2 + outputQuant1 + outputQuant2 + crc1 + crc2</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>elif</span> <span class='nam'>function_code</span> <span class='op'>==</span> <span class='nam'>defines</span><span class='op'>.</span><span class='nam'>WRITE_MULTIPLE_REGISTERS</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>byte_count</span> <span class='op'>=</span> <span class='num'>2</span> <span class='op'>*</span> <span class='nam'>len</span><span class='op'>(</span><span class='nam'>output_value</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>pdu</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;BHHB&quot;</span><span class='op'>,</span> <span class='nam'>function_code</span><span class='op'>,</span> <span class='nam'>starting_address</span><span class='op'>,</span> <span class='nam'>len</span><span class='op'>(</span><span class='nam'>output_value</span><span class='op'>)</span><span class='op'>,</span> <span class='nam'>byte_count</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>j</span> <span class='key'>in</span> <span class='nam'>output_value</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>pdu</span> <span class='op'>+=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;H&quot;</span><span class='op'>,</span> <span class='nam'>j</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>282</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='key'>not</span> <span class='nam'>data_format</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>data_format</span> <span class='op'>=</span> <span class='str'>&quot;&gt;HH&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>289</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>expected_length</span> <span class='op'>&lt;</span> <span class='num'>0</span><span class='op'>:</span>&nbsp; <span class='com'>#No lenght was specified and calculated length can be used:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>expected_length</span> <span class='op'>=</span> <span class='num'>8</span>&nbsp; <span class='com'>#slave + func + adress1 + adress2 + outputQuant1 + outputQuant2 + crc1 + crc2</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>else</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>ModbusFunctionNotSupportedError</span><span class='op'>(</span><span class='str'>&quot;The %d function code is not supported. &quot;</span> <span class='op'>%</span> <span class='op'>(</span><span class='nam'>function_code</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># instantiate a query which implements the MAC (TCP or RTU) part of the protocol</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>query</span> <span class='op'>=</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_make_query</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># add the mac part of the protocol to the request</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>request</span> <span class='op'>=</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>build_request</span><span class='op'>(</span><span class='nam'>pdu</span><span class='op'>,</span> <span class='nam'>slave</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># send the request to the slave</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>retval</span> <span class='op'>=</span> <span class='nam'>call_hooks</span><span class='op'>(</span><span class='str'>&quot;modbus.Master.before_send&quot;</span><span class='op'>,</span> <span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>request</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>297</span>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>retval</span> <span class='op'>&lt;&gt;</span> <span class='nam'>None</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>request</span> <span class='op'>=</span> <span class='nam'>retval</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>299</span>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_verbose</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>LOGGER</span><span class='op'>.</span><span class='nam'>debug</span><span class='op'>(</span><span class='nam'>utils</span><span class='op'>.</span><span class='nam'>get_log_buffer</span><span class='op'>(</span><span class='str'>&quot;-&gt; &quot;</span><span class='op'>,</span> <span class='nam'>request</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_send</span><span class='op'>(</span><span class='nam'>request</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>call_hooks</span><span class='op'>(</span><span class='str'>&quot;modbus.Master.after_send&quot;</span><span class='op'>,</span> <span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>slave</span> <span class='op'>!=</span> <span class='num'>0</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># receive the data from the slave</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>response</span> <span class='op'>=</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_recv</span><span class='op'>(</span><span class='nam'>expected_length</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>retval</span> <span class='op'>=</span> <span class='nam'>call_hooks</span><span class='op'>(</span><span class='str'>&quot;modbus.Master.after_recv&quot;</span><span class='op'>,</span> <span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>response</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>309</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>retval</span> <span class='op'>&lt;&gt;</span> <span class='nam'>None</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>response</span> <span class='op'>=</span> <span class='nam'>retval</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>311</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_verbose</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>LOGGER</span><span class='op'>.</span><span class='nam'>debug</span><span class='op'>(</span><span class='nam'>utils</span><span class='op'>.</span><span class='nam'>get_log_buffer</span><span class='op'>(</span><span class='str'>&quot;&lt;- &quot;</span><span class='op'>,</span> <span class='nam'>response</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># extract the pdu part of the response</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>response_pdu</span> <span class='op'>=</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>parse_response</span><span class='op'>(</span><span class='nam'>response</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># analyze the received data</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='op'>(</span><span class='nam'>return_code</span><span class='op'>,</span> <span class='nam'>byte_2</span><span class='op'>)</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>unpack</span><span class='op'>(</span><span class='str'>&quot;&gt;BB&quot;</span><span class='op'>,</span> <span class='nam'>response_pdu</span><span class='op'>[</span><span class='num'>0</span><span class='op'>:</span><span class='num'>2</span><span class='op'>]</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>return_code</span> <span class='op'>&gt;</span> <span class='num'>0x80</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># the slave has returned an error</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>exception_code</span> <span class='op'>=</span> <span class='nam'>byte_2</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>ModbusError</span><span class='op'>(</span><span class='nam'>exception_code</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>else</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>is_read_function</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># get the values returned by the reading function</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>byte_count</span> <span class='op'>=</span> <span class='nam'>byte_2</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>data</span> <span class='op'>=</span> <span class='nam'>response_pdu</span><span class='op'>[</span><span class='num'>2</span><span class='op'>:</span><span class='op'>]</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>330</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>byte_count</span> <span class='op'>!=</span> <span class='nam'>len</span><span class='op'>(</span><span class='nam'>data</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># the byte count in the pdu is invalid</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>ModbusInvalidResponseError</span><span class='op'>,</span> <span class='str'>&quot;Byte count is %d while actual number of bytes is %d. &quot;</span><span class='op'>%</span> <span class='op'>(</span><span class='nam'>byte_count</span><span class='op'>,</span> <span class='nam'>len</span><span class='op'>(</span><span class='nam'>data</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>else</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># returns what is returned by the slave after a writing function</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>data</span> <span class='op'>=</span> <span class='nam'>response_pdu</span><span class='op'>[</span><span class='num'>1</span><span class='op'>:</span><span class='op'>]</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'>#returns the data as a tuple according to the data_format</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'>#(calculated based on the function or user-defined)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>result</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>unpack</span><span class='op'>(</span><span class='nam'>data_format</span><span class='op'>,</span> <span class='nam'>data</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>nb_of_digits</span> <span class='op'>&gt;</span> <span class='num'>0</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>digits</span> <span class='op'>=</span> <span class='op'>[</span><span class='op'>]</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>byte_val</span> <span class='key'>in</span> <span class='nam'>result</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>i</span> <span class='key'>in</span> <span class='nam'>xrange</span><span class='op'>(</span><span class='num'>8</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='op'>(</span><span class='nam'>len</span><span class='op'>(</span><span class='nam'>digits</span><span class='op'>)</span> <span class='op'>&gt;=</span> <span class='nam'>nb_of_digits</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>break</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>digits</span><span class='op'>.</span><span class='nam'>append</span><span class='op'>(</span><span class='nam'>byte_val</span> <span class='op'>%</span> <span class='num'>2</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>byte_val</span> <span class='op'>=</span> <span class='nam'>byte_val</span> <span class='op'>&gt;&gt;</span> <span class='num'>1</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>result</span> <span class='op'>=</span> <span class='nam'>tuple</span><span class='op'>(</span><span class='nam'>digits</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>result</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>set_timeout</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>timeout_in_sec</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Defines a timeout on the MAC layer&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_timeout</span> <span class='op'>=</span> <span class='nam'>timeout_in_sec</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>get_timeout</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Gets the current value of the MAC layer timeout&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_timeout</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='com'>#-------------------------------------------------------------------------------</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='key'>class</span> <span class='nam'>ModbusBlock</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;This class represents the values for a range of addresses&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>__init__</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>starting_address</span><span class='op'>,</span> <span class='nam'>size</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; Contructor: defines the address range and creates the array of values</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>starting_address</span> <span class='op'>=</span> <span class='nam'>starting_address</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>data</span> <span class='op'>=</span> <span class='op'>[</span><span class='num'>0</span><span class='op'>]</span><span class='op'>*</span><span class='nam'>size</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>size</span> <span class='op'>=</span> <span class='nam'>len</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>data</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>is_in</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>starting_address</span><span class='op'>,</span> <span class='nam'>size</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; Returns true if a block with the given address and size</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; would overlap this block</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>starting_address</span> <span class='op'>&gt;</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>starting_address</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>377</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='op'>(</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>starting_address</span><span class='op'>+</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>size</span><span class='op'>)</span><span class='op'>&gt;</span><span class='nam'>starting_address</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>elif</span> <span class='nam'>starting_address</span> <span class='op'>&lt;</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>starting_address</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='op'>(</span><span class='nam'>starting_address</span><span class='op'>+</span><span class='nam'>size</span><span class='op'>)</span><span class='op'>&gt;</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>starting_address</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>True</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='com'>#-------------------------------------------------------------------------------</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='key'>class</span> <span class='nam'>Slave</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; This class define a modbus slave which is in charge of making the action</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; asked by a modbus query</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>__init__</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>id</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Constructor&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_id</span> <span class='op'>=</span> <span class='nam'>id</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_blocks</span> <span class='op'>=</span> <span class='op'>{</span><span class='op'>}</span> <span class='com'># the map registring all blocks of the slave</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># a shortcut to find blocks per type</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_memory</span> <span class='op'>=</span> <span class='op'>{</span><span class='num'>1</span><span class='op'>:</span><span class='op'>[</span><span class='op'>]</span><span class='op'>,</span> <span class='num'>2</span><span class='op'>:</span><span class='op'>[</span><span class='op'>]</span><span class='op'>,</span> <span class='num'>3</span><span class='op'>:</span><span class='op'>[</span><span class='op'>]</span><span class='op'>,</span> <span class='num'>4</span><span class='op'>:</span><span class='op'>[</span><span class='op'>]</span><span class='op'>}</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># a lock for mutual access to the _blocks and _memory maps</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_data_lock</span> <span class='op'>=</span> <span class='nam'>threading</span><span class='op'>.</span><span class='nam'>Lock</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='com'>#map modbus function code to a function:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_fn_code_map</span> <span class='op'>=</span> <span class='op'>{</span><span class='nam'>defines</span><span class='op'>.</span><span class='nam'>READ_COILS</span><span class='op'>:</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_read_coils</span><span class='op'>,</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; <span class='nam'>defines</span><span class='op'>.</span><span class='nam'>READ_DISCRETE_INPUTS</span><span class='op'>:</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_read_discrete_inputs</span><span class='op'>,</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; <span class='nam'>defines</span><span class='op'>.</span><span class='nam'>READ_INPUT_REGISTERS</span><span class='op'>:</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_read_input_registers</span><span class='op'>,</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; <span class='nam'>defines</span><span class='op'>.</span><span class='nam'>READ_HOLDING_REGISTERS</span><span class='op'>:</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_read_holding_registers</span><span class='op'>,</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; <span class='nam'>defines</span><span class='op'>.</span><span class='nam'>WRITE_SINGLE_COIL</span><span class='op'>:</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_write_single_coil</span><span class='op'>,</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; <span class='nam'>defines</span><span class='op'>.</span><span class='nam'>WRITE_SINGLE_REGISTER</span><span class='op'>:</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_write_single_register</span><span class='op'>,</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; <span class='nam'>defines</span><span class='op'>.</span><span class='nam'>WRITE_MULTIPLE_COILS</span><span class='op'>:</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_write_multiple_coils</span><span class='op'>,</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; <span class='nam'>defines</span><span class='op'>.</span><span class='nam'>WRITE_MULTIPLE_REGISTERS</span><span class='op'>:</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_write_multiple_registers</span><span class='op'>,</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; <span class='op'>}</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_get_block_and_offset</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>block_type</span><span class='op'>,</span> <span class='nam'>address</span><span class='op'>,</span> <span class='nam'>length</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;returns the block and offset corresponding to the given address&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>block</span> <span class='key'>in</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_memory</span><span class='op'>[</span><span class='nam'>block_type</span><span class='op'>]</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>address</span> <span class='op'>&gt;=</span> <span class='nam'>block</span><span class='op'>.</span><span class='nam'>starting_address</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>offset</span> <span class='op'>=</span> <span class='nam'>address</span> <span class='op'>-</span> <span class='nam'>block</span><span class='op'>.</span><span class='nam'>starting_address</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>block</span><span class='op'>.</span><span class='nam'>size</span> <span class='op'>&gt;=</span> <span class='nam'>offset</span><span class='op'>+</span><span class='nam'>length</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>block</span><span class='op'>,</span> <span class='nam'>offset</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>ModbusError</span><span class='op'>(</span><span class='nam'>defines</span><span class='op'>.</span><span class='nam'>ILLEGAL_DATA_ADDRESS</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_read_digital</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>block_type</span><span class='op'>,</span> <span class='nam'>request_pdu</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;read the value of coils and discrete inputs&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='op'>(</span><span class='nam'>starting_address</span><span class='op'>,</span> <span class='nam'>quantity_of_x</span><span class='op'>)</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>unpack</span><span class='op'>(</span><span class='str'>&quot;&gt;HH&quot;</span><span class='op'>,</span> <span class='nam'>request_pdu</span><span class='op'>[</span><span class='num'>1</span><span class='op'>:</span><span class='num'>5</span><span class='op'>]</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='op'>(</span><span class='nam'>quantity_of_x</span> <span class='op'>&lt;=</span> <span class='num'>0</span><span class='op'>)</span> <span class='key'>or</span> <span class='op'>(</span><span class='nam'>quantity_of_x</span><span class='op'>&gt;</span><span class='num'>2000</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># maximum allowed size is 2000 bits in one reading</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>ModbusError</span><span class='op'>(</span><span class='nam'>defines</span><span class='op'>.</span><span class='nam'>ILLEGAL_DATA_VALUE</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>block</span><span class='op'>,</span> <span class='nam'>offset</span> <span class='op'>=</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_get_block_and_offset</span><span class='op'>(</span><span class='nam'>block_type</span><span class='op'>,</span> <span class='nam'>starting_address</span><span class='op'>,</span> <span class='nam'>quantity_of_x</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>values</span> <span class='op'>=</span> <span class='nam'>block</span><span class='op'>.</span><span class='nam'>data</span><span class='op'>[</span><span class='nam'>offset</span><span class='op'>:</span><span class='nam'>offset</span><span class='op'>+</span><span class='nam'>quantity_of_x</span><span class='op'>]</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='com'>#pack bits in bytes</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>byte_count</span> <span class='op'>=</span> <span class='nam'>quantity_of_x</span> <span class='op'>/</span> <span class='num'>8</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='op'>(</span><span class='nam'>quantity_of_x</span> <span class='op'>%</span> <span class='num'>8</span><span class='op'>)</span> <span class='op'>&gt;</span> <span class='num'>0</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>byte_count</span> <span class='op'>+=</span> <span class='num'>1</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># write the response header</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>response</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;B&quot;</span><span class='op'>,</span> <span class='nam'>byte_count</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>i</span><span class='op'>,</span> <span class='nam'>byte_value</span> <span class='op'>=</span> <span class='num'>0</span><span class='op'>,</span> <span class='num'>0</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>coil</span> <span class='key'>in</span> <span class='nam'>values</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>coil</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>byte_value</span> <span class='op'>+=</span> <span class='op'>(</span><span class='num'>1</span> <span class='op'>&lt;&lt;</span> <span class='nam'>i</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>i</span> <span class='op'>&gt;=</span> <span class='num'>7</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># write the values of 8 bits in a byte</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>response</span> <span class='op'>+=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;B&quot;</span><span class='op'>,</span> <span class='nam'>byte_value</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'>#reset the counters</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>i</span><span class='op'>,</span> <span class='nam'>byte_value</span> <span class='op'>=</span> <span class='num'>0</span><span class='op'>,</span> <span class='num'>0</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>else</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>i</span> <span class='op'>+=</span> <span class='num'>1</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='com'>#if there is remaining bits: add one more byte with their values</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>i</span> <span class='op'>&gt;</span> <span class='num'>0</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>response</span> <span class='op'>+=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;B&quot;</span><span class='op'>,</span> <span class='nam'>byte_value</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>response</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_read_coils</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>request_pdu</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;handle read coils modbus function&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_read_digital</span><span class='op'>(</span><span class='nam'>defines</span><span class='op'>.</span><span class='nam'>COILS</span><span class='op'>,</span> <span class='nam'>request_pdu</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_read_discrete_inputs</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>request_pdu</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;handle read coils modbus function&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_read_digital</span><span class='op'>(</span><span class='nam'>defines</span><span class='op'>.</span><span class='nam'>DISCRETE_INPUTS</span><span class='op'>,</span> <span class='nam'>request_pdu</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_read_registers</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>block_type</span><span class='op'>,</span> <span class='nam'>request_pdu</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;read the value of holding and input registers&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='op'>(</span><span class='nam'>starting_address</span><span class='op'>,</span> <span class='nam'>quantity_of_x</span><span class='op'>)</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>unpack</span><span class='op'>(</span><span class='str'>&quot;&gt;HH&quot;</span><span class='op'>,</span> <span class='nam'>request_pdu</span><span class='op'>[</span><span class='num'>1</span><span class='op'>:</span><span class='num'>5</span><span class='op'>]</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='op'>(</span><span class='nam'>quantity_of_x</span> <span class='op'>&lt;=</span> <span class='num'>0</span><span class='op'>)</span> <span class='key'>or</span> <span class='op'>(</span><span class='nam'>quantity_of_x</span> <span class='op'>&gt;</span> <span class='num'>125</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># maximum allowed size is 125 registers in one reading</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>LOGGER</span><span class='op'>.</span><span class='nam'>debug</span><span class='op'>(</span><span class='str'>&quot;quantity_of_x is %d&quot;</span> <span class='op'>%</span> <span class='nam'>quantity_of_x</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>ModbusError</span><span class='op'>(</span><span class='nam'>defines</span><span class='op'>.</span><span class='nam'>ILLEGAL_DATA_VALUE</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># look for the block corresponding to the request</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>block</span><span class='op'>,</span> <span class='nam'>offset</span> <span class='op'>=</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_get_block_and_offset</span><span class='op'>(</span><span class='nam'>block_type</span><span class='op'>,</span> <span class='nam'>starting_address</span><span class='op'>,</span> <span class='nam'>quantity_of_x</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='com'>#get the values</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>values</span> <span class='op'>=</span> <span class='nam'>block</span><span class='op'>.</span><span class='nam'>data</span><span class='op'>[</span><span class='nam'>offset</span><span class='op'>:</span><span class='nam'>offset</span><span class='op'>+</span><span class='nam'>quantity_of_x</span><span class='op'>]</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='com'>#write the response header</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>response</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;B&quot;</span><span class='op'>,</span> <span class='num'>2</span> <span class='op'>*</span> <span class='nam'>quantity_of_x</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='com'>#add the values of every register on 2 bytes</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>reg</span> <span class='key'>in</span> <span class='nam'>values</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>response</span> <span class='op'>+=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;H&quot;</span><span class='op'>,</span> <span class='nam'>reg</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>response</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_read_holding_registers</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>request_pdu</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;handle read coils modbus function&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_read_registers</span><span class='op'>(</span><span class='nam'>defines</span><span class='op'>.</span><span class='nam'>HOLDING_REGISTERS</span><span class='op'>,</span> <span class='nam'>request_pdu</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_read_input_registers</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>request_pdu</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;handle read coils modbus function&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_read_registers</span><span class='op'>(</span><span class='nam'>defines</span><span class='op'>.</span><span class='nam'>ANALOG_INPUTS</span><span class='op'>,</span> <span class='nam'>request_pdu</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_write_multiple_registers</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>request_pdu</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;execute modbus function 16&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># get the starting address and the number of items from the request pdu</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='op'>(</span><span class='nam'>starting_address</span><span class='op'>,</span> <span class='nam'>quantity_of_x</span><span class='op'>,</span> <span class='nam'>byte_count</span><span class='op'>)</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>unpack</span><span class='op'>(</span><span class='str'>&quot;&gt;HHB&quot;</span><span class='op'>,</span> <span class='nam'>request_pdu</span><span class='op'>[</span><span class='num'>1</span><span class='op'>:</span><span class='num'>6</span><span class='op'>]</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='op'>(</span><span class='nam'>quantity_of_x</span> <span class='op'>&lt;=</span> <span class='num'>0</span><span class='op'>)</span> <span class='key'>or</span> <span class='op'>(</span><span class='nam'>quantity_of_x</span> <span class='op'>&gt;</span> <span class='num'>123</span><span class='op'>)</span> <span class='key'>or</span> <span class='op'>(</span><span class='nam'>byte_count</span> <span class='op'>!=</span> <span class='op'>(</span><span class='nam'>quantity_of_x</span> <span class='op'>*</span> <span class='num'>2</span><span class='op'>)</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># maximum allowed size is 123 registers in one reading</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>ModbusError</span><span class='op'>(</span><span class='nam'>defines</span><span class='op'>.</span><span class='nam'>ILLEGAL_DATA_VALUE</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># look for the block corresponding to the request</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>block</span><span class='op'>,</span> <span class='nam'>offset</span> <span class='op'>=</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_get_block_and_offset</span><span class='op'>(</span><span class='nam'>defines</span><span class='op'>.</span><span class='nam'>HOLDING_REGISTERS</span><span class='op'>,</span> <span class='nam'>starting_address</span><span class='op'>,</span> <span class='nam'>quantity_of_x</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>count</span> <span class='op'>=</span> <span class='num'>0</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>i</span> <span class='key'>in</span> <span class='nam'>xrange</span><span class='op'>(</span><span class='nam'>quantity_of_x</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>count</span> <span class='op'>+=</span> <span class='num'>1</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>block</span><span class='op'>.</span><span class='nam'>data</span><span class='op'>[</span><span class='nam'>offset</span><span class='op'>+</span><span class='nam'>i</span><span class='op'>]</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>unpack</span><span class='op'>(</span><span class='str'>&quot;&gt;H&quot;</span><span class='op'>,</span> <span class='nam'>request_pdu</span><span class='op'>[</span><span class='num'>6</span><span class='op'>+</span><span class='num'>2</span><span class='op'>*</span><span class='nam'>i</span><span class='op'>:</span><span class='num'>8</span><span class='op'>+</span><span class='num'>2</span><span class='op'>*</span><span class='nam'>i</span><span class='op'>]</span><span class='op'>)</span><span class='op'>[</span><span class='num'>0</span><span class='op'>]</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;HH&quot;</span><span class='op'>,</span> <span class='nam'>starting_address</span><span class='op'>,</span> <span class='nam'>count</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_write_multiple_coils</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>request_pdu</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;execute modbus function 15&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># get the starting address and the number of items from the request pdu</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='op'>(</span><span class='nam'>starting_address</span><span class='op'>,</span> <span class='nam'>quantity_of_x</span><span class='op'>,</span> <span class='nam'>byte_count</span><span class='op'>)</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>unpack</span><span class='op'>(</span><span class='str'>&quot;&gt;HHB&quot;</span><span class='op'>,</span> <span class='nam'>request_pdu</span><span class='op'>[</span><span class='num'>1</span><span class='op'>:</span><span class='num'>6</span><span class='op'>]</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>expected_byte_count</span> <span class='op'>=</span> <span class='nam'>quantity_of_x</span> <span class='op'>/</span> <span class='num'>8</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='op'>(</span><span class='nam'>quantity_of_x</span> <span class='op'>%</span> <span class='num'>8</span><span class='op'>)</span> <span class='op'>&gt;</span> <span class='num'>0</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>expected_byte_count</span> <span class='op'>+=</span> <span class='num'>1</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='op'>(</span><span class='nam'>quantity_of_x</span> <span class='op'>&lt;=</span> <span class='num'>0</span><span class='op'>)</span> <span class='key'>or</span> <span class='op'>(</span><span class='nam'>quantity_of_x</span> <span class='op'>&gt;</span> <span class='num'>1968</span><span class='op'>)</span> <span class='key'>or</span> <span class='op'>(</span><span class='nam'>byte_count</span> <span class='op'>!=</span> <span class='nam'>expected_byte_count</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># maximum allowed size is 1968 coils</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>ModbusError</span><span class='op'>(</span><span class='nam'>defines</span><span class='op'>.</span><span class='nam'>ILLEGAL_DATA_VALUE</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># look for the block corresponding to the request</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>block</span><span class='op'>,</span> <span class='nam'>offset</span> <span class='op'>=</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_get_block_and_offset</span><span class='op'>(</span><span class='nam'>defines</span><span class='op'>.</span><span class='nam'>COILS</span><span class='op'>,</span> <span class='nam'>starting_address</span><span class='op'>,</span> <span class='nam'>quantity_of_x</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>count</span> <span class='op'>=</span> <span class='num'>0</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>i</span> <span class='key'>in</span> <span class='nam'>xrange</span><span class='op'>(</span><span class='nam'>byte_count</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>count</span> <span class='op'>&gt;=</span> <span class='nam'>quantity_of_x</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>530</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>break</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='op'>(</span><span class='nam'>byte_value</span><span class='op'>,</span> <span class='op'>)</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>unpack</span><span class='op'>(</span><span class='str'>&quot;&gt;B&quot;</span><span class='op'>,</span> <span class='nam'>request_pdu</span><span class='op'>[</span><span class='num'>6</span><span class='op'>+</span><span class='nam'>i</span><span class='op'>]</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>j</span> <span class='key'>in</span> <span class='nam'>xrange</span><span class='op'>(</span><span class='num'>8</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>byte_value</span> <span class='op'>&amp;</span> <span class='op'>(</span><span class='num'>1</span> <span class='op'>&lt;&lt;</span> <span class='nam'>j</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>block</span><span class='op'>.</span><span class='nam'>data</span><span class='op'>[</span><span class='nam'>offset</span><span class='op'>+</span><span class='nam'>i</span><span class='op'>*</span><span class='num'>8</span><span class='op'>+</span><span class='nam'>j</span><span class='op'>]</span> <span class='op'>=</span> <span class='num'>1</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>else</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>block</span><span class='op'>.</span><span class='nam'>data</span><span class='op'>[</span><span class='nam'>offset</span><span class='op'>+</span><span class='nam'>i</span><span class='op'>*</span><span class='num'>8</span><span class='op'>+</span><span class='nam'>j</span><span class='op'>]</span> <span class='op'>=</span> <span class='num'>0</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>count</span> <span class='op'>&gt;=</span> <span class='nam'>quantity_of_x</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>break</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>count</span> <span class='op'>+=</span> <span class='num'>1</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;HH&quot;</span><span class='op'>,</span> <span class='nam'>starting_address</span><span class='op'>,</span> <span class='nam'>count</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_write_single_register</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>request_pdu</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;execute modbus function 6&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='op'>(</span><span class='nam'>data_address</span><span class='op'>,</span> <span class='nam'>value</span><span class='op'>)</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>unpack</span><span class='op'>(</span><span class='str'>&quot;&gt;HH&quot;</span><span class='op'>,</span> <span class='nam'>request_pdu</span><span class='op'>[</span><span class='num'>1</span><span class='op'>:</span><span class='num'>5</span><span class='op'>]</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>block</span><span class='op'>,</span> <span class='nam'>offset</span> <span class='op'>=</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_get_block_and_offset</span><span class='op'>(</span><span class='nam'>defines</span><span class='op'>.</span><span class='nam'>HOLDING_REGISTERS</span><span class='op'>,</span> <span class='nam'>data_address</span><span class='op'>,</span> <span class='num'>1</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>block</span><span class='op'>.</span><span class='nam'>data</span><span class='op'>[</span><span class='nam'>offset</span><span class='op'>]</span> <span class='op'>=</span> <span class='nam'>value</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>request_pdu</span><span class='op'>[</span><span class='num'>1</span><span class='op'>:</span><span class='op'>]</span> <span class='com'>#returns echo of the command</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_write_single_coil</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>request_pdu</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;execute modbus function 5&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='op'>(</span><span class='nam'>data_address</span><span class='op'>,</span> <span class='nam'>value</span><span class='op'>)</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>unpack</span><span class='op'>(</span><span class='str'>&quot;&gt;HH&quot;</span><span class='op'>,</span> <span class='nam'>request_pdu</span><span class='op'>[</span><span class='num'>1</span><span class='op'>:</span><span class='num'>5</span><span class='op'>]</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>block</span><span class='op'>,</span> <span class='nam'>offset</span> <span class='op'>=</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_get_block_and_offset</span><span class='op'>(</span><span class='nam'>defines</span><span class='op'>.</span><span class='nam'>COILS</span><span class='op'>,</span> <span class='nam'>data_address</span><span class='op'>,</span> <span class='num'>1</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>value</span> <span class='op'>==</span> <span class='num'>0</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>block</span><span class='op'>.</span><span class='nam'>data</span><span class='op'>[</span><span class='nam'>offset</span><span class='op'>]</span> <span class='op'>=</span> <span class='num'>0</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>elif</span> <span class='nam'>value</span> <span class='op'>==</span> <span class='num'>0xff00</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>block</span><span class='op'>.</span><span class='nam'>data</span><span class='op'>[</span><span class='nam'>offset</span><span class='op'>]</span> <span class='op'>=</span> <span class='num'>1</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>else</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>ModbusError</span><span class='op'>(</span><span class='nam'>defines</span><span class='op'>.</span><span class='nam'>ILLEGAL_DATA_VALUE</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>request_pdu</span><span class='op'>[</span><span class='num'>1</span><span class='op'>:</span><span class='op'>]</span> <span class='com'>#returns echo of the command</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>handle_request</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>request_pdu</span><span class='op'>,</span> <span class='nam'>broadcast</span><span class='op'>=</span><span class='nam'>False</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; parse the request pdu, makes the corresponding action</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; and returns the response pdu</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>with</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_data_lock</span><span class='op'>:</span> <span class='com'>#thread-safe</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>try</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>retval</span> <span class='op'>=</span> <span class='nam'>call_hooks</span><span class='op'>(</span><span class='str'>&quot;modbus.Slave.handle_request&quot;</span><span class='op'>,</span> <span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>request_pdu</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>retval</span> <span class='op'>&lt;&gt;</span> <span class='nam'>None</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>570</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>retval</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># get the function code</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='op'>(</span><span class='nam'>function_code</span><span class='op'>,</span> <span class='op'>)</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>unpack</span><span class='op'>(</span><span class='str'>&quot;&gt;B&quot;</span><span class='op'>,</span> <span class='nam'>request_pdu</span><span class='op'>[</span><span class='num'>0</span><span class='op'>]</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># check if the function code is valid. If not returns error response</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='key'>not</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_fn_code_map</span><span class='op'>.</span><span class='nam'>has_key</span><span class='op'>(</span><span class='nam'>function_code</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>577</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>ModbusError</span><span class='op'>(</span><span class='nam'>defines</span><span class='op'>.</span><span class='nam'>ILLEGAL_FUNCTION</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># if read query is broadcasted raises an error</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>cant_be_broadcasted</span> <span class='op'>=</span> <span class='op'>(</span><span class='nam'>defines</span><span class='op'>.</span><span class='nam'>READ_COILS</span><span class='op'>,</span> <span class='nam'>defines</span><span class='op'>.</span><span class='nam'>READ_DISCRETE_INPUTS</span><span class='op'>,</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>defines</span><span class='op'>.</span><span class='nam'>READ_INPUT_REGISTERS</span><span class='op'>,</span> <span class='nam'>defines</span><span class='op'>.</span><span class='nam'>READ_HOLDING_REGISTERS</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>broadcast</span> <span class='key'>and</span> <span class='op'>(</span><span class='nam'>function_code</span> <span class='key'>in</span> <span class='nam'>cant_be_broadcasted</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>ModbusInvalidRequestError</span><span class='op'>(</span><span class='str'>&quot;Function %d can not be broadcasted&quot;</span> <span class='op'>%</span> <span class='nam'>function_code</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'>#execute the corresponding function</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>response_pdu</span> <span class='op'>=</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_fn_code_map</span><span class='op'>[</span><span class='nam'>function_code</span><span class='op'>]</span><span class='op'>(</span><span class='nam'>request_pdu</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>response_pdu</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>594</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>broadcast</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>call_hooks</span><span class='op'>(</span><span class='str'>&quot;modbus.Slave.on_handle_broadcast&quot;</span><span class='op'>,</span> <span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>response_pdu</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>LOGGER</span><span class='op'>.</span><span class='nam'>debug</span><span class='op'>(</span><span class='str'>&quot;broadcast: %s&quot;</span> <span class='op'>%</span> <span class='op'>(</span><span class='nam'>utils</span><span class='op'>.</span><span class='nam'>get_log_buffer</span><span class='op'>(</span><span class='str'>&quot;!!&quot;</span><span class='op'>,</span> <span class='nam'>response_pdu</span><span class='op'>)</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='str'>&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>else</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;B&quot;</span><span class='op'>,</span> <span class='nam'>function_code</span><span class='op'>)</span> <span class='op'>+</span> <span class='nam'>response_pdu</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>Exception</span><span class='op'>(</span><span class='str'>&quot;No response for function %d&quot;</span> <span class='op'>%</span> <span class='nam'>function_code</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>except</span> <span class='nam'>ModbusError</span><span class='op'>,</span> <span class='nam'>excpt</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>LOGGER</span><span class='op'>.</span><span class='nam'>debug</span><span class='op'>(</span><span class='nam'>str</span><span class='op'>(</span><span class='nam'>excpt</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>call_hooks</span><span class='op'>(</span><span class='str'>&quot;modbus.Slave.on_exception&quot;</span><span class='op'>,</span> <span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>function_code</span><span class='op'>,</span> <span class='nam'>excpt</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;BB&quot;</span><span class='op'>,</span> <span class='nam'>function_code</span><span class='op'>+</span><span class='num'>128</span><span class='op'>,</span> <span class='nam'>excpt</span><span class='op'>.</span><span class='nam'>get_exception_code</span><span class='op'>(</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>add_block</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>block_name</span><span class='op'>,</span> <span class='nam'>block_type</span><span class='op'>,</span> <span class='nam'>starting_address</span><span class='op'>,</span> <span class='nam'>size</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Add a new block identified by its name&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>with</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_data_lock</span><span class='op'>:</span> <span class='com'>#thread-safe</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>size</span> <span class='op'>&lt;=</span> <span class='num'>0</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>606</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>InvalidArgumentError</span><span class='op'>,</span> <span class='str'>&quot;size must be a positive number&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>starting_address</span> <span class='op'>&lt;</span> <span class='num'>0</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>608</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>InvalidArgumentError</span><span class='op'>,</span> <span class='str'>&quot;starting address must be zero or positive number&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_blocks</span><span class='op'>.</span><span class='nam'>has_key</span><span class='op'>(</span><span class='nam'>block_name</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>610</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>DuplicatedKeyError</span><span class='op'>,</span> <span class='str'>&quot;Block %s already exists. &quot;</span> <span class='op'>%</span> <span class='op'>(</span><span class='nam'>block_name</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='key'>not</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_memory</span><span class='op'>.</span><span class='nam'>has_key</span><span class='op'>(</span><span class='nam'>block_type</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>613</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>InvalidModbusBlockError</span><span class='op'>,</span> <span class='str'>&quot;Invalid block type %d&quot;</span> <span class='op'>%</span> <span class='op'>(</span><span class='nam'>block_type</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># check that the new block doesn&#39;t overlap an existing block</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># it means that only 1 block per type must correspond to a given address</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># for example: it must not have 2 holding registers at address 100</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>index</span> <span class='op'>=</span> <span class='num'>0</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>i</span> <span class='key'>in</span> <span class='nam'>xrange</span><span class='op'>(</span><span class='nam'>len</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>_memory</span><span class='op'>[</span><span class='nam'>block_type</span><span class='op'>]</span><span class='op'>)</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>block</span> <span class='op'>=</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_memory</span><span class='op'>[</span><span class='nam'>block_type</span><span class='op'>]</span><span class='op'>[</span><span class='nam'>i</span><span class='op'>]</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>block</span><span class='op'>.</span><span class='nam'>is_in</span><span class='op'>(</span><span class='nam'>starting_address</span><span class='op'>,</span> <span class='nam'>size</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>622</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>OverlapModbusBlockError</span><span class='op'>,</span> <span class='str'>&quot;Overlap block at %d size %d&quot;</span> <span class='op'>%</span> <span class='op'>(</span><span class='nam'>block</span><span class='op'>.</span><span class='nam'>starting_address</span><span class='op'>,</span> <span class='nam'>block</span><span class='op'>.</span><span class='nam'>size</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>block</span><span class='op'>.</span><span class='nam'>starting_address</span> <span class='op'>&gt;</span> <span class='nam'>starting_address</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>624</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>index</span> <span class='op'>=</span> <span class='nam'>i</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>break</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># if the block is ok: register it</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_blocks</span><span class='op'>[</span><span class='nam'>block_name</span><span class='op'>]</span> <span class='op'>=</span> <span class='op'>(</span><span class='nam'>block_type</span><span class='op'>,</span> <span class='nam'>starting_address</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># add it in the &#39;per type&#39; shortcut </span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_memory</span><span class='op'>[</span><span class='nam'>block_type</span><span class='op'>]</span><span class='op'>.</span><span class='nam'>insert</span><span class='op'>(</span><span class='nam'>index</span><span class='op'>,</span> <span class='nam'>ModbusBlock</span><span class='op'>(</span><span class='nam'>starting_address</span><span class='op'>,</span> <span class='nam'>size</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>remove_block</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>block_name</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; Remove the block with the given name.</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; Raise an exception if not found</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>with</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_data_lock</span><span class='op'>:</span> <span class='com'>#thread safe</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>block</span> <span class='op'>=</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_get_block</span><span class='op'>(</span><span class='nam'>block_name</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># the block has been found: remove it from the shortcut</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>block_type</span> <span class='op'>=</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_blocks</span><span class='op'>.</span><span class='nam'>pop</span><span class='op'>(</span><span class='nam'>block_name</span><span class='op'>)</span><span class='op'>[</span><span class='num'>0</span><span class='op'>]</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_memory</span><span class='op'>[</span><span class='nam'>block_type</span><span class='op'>]</span><span class='op'>.</span><span class='nam'>remove</span><span class='op'>(</span><span class='nam'>block</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>remove_all_blocks</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; Remove all the blocks</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>with</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_data_lock</span><span class='op'>:</span> <span class='com'>#thread safe</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_blocks</span><span class='op'>.</span><span class='nam'>clear</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>key</span> <span class='key'>in</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_memory</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_memory</span><span class='op'>[</span><span class='nam'>key</span><span class='op'>]</span> <span class='op'>=</span> <span class='op'>[</span><span class='op'>]</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_get_block</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>block_name</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Find a block by its name and raise and exception if not found&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='key'>not</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_blocks</span><span class='op'>.</span><span class='nam'>has_key</span><span class='op'>(</span><span class='nam'>block_name</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>656</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>MissingKeyError</span><span class='op'>,</span> <span class='str'>&quot;block %s not found&quot;</span> <span class='op'>%</span> <span class='op'>(</span><span class='nam'>block_name</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='op'>(</span><span class='nam'>block_type</span><span class='op'>,</span> <span class='nam'>starting_address</span><span class='op'>)</span> <span class='op'>=</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_blocks</span><span class='op'>[</span><span class='nam'>block_name</span><span class='op'>]</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>block</span> <span class='key'>in</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_memory</span><span class='op'>[</span><span class='nam'>block_type</span><span class='op'>]</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>661</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>block</span><span class='op'>.</span><span class='nam'>starting_address</span> <span class='op'>==</span> <span class='nam'>starting_address</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>block</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>Exception</span><span class='op'>,</span> <span class='str'>&quot;Bug?: the block %s is not registered properly in memory&quot;</span> <span class='op'>%</span> <span class='op'>(</span><span class='nam'>block_name</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>set_values</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>block_name</span><span class='op'>,</span> <span class='nam'>address</span><span class='op'>,</span> <span class='nam'>values</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; Set the values of the items at the given address</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; If values is a list or a tuple, the value of every item is written</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; If values is a number, only one value is written</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>with</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_data_lock</span><span class='op'>:</span> <span class='com'>#thread safe</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>block</span> <span class='op'>=</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_get_block</span><span class='op'>(</span><span class='nam'>block_name</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># the block has been found</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># check that it doesn&#39;t write out of the block</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>offset</span> <span class='op'>=</span> <span class='nam'>address</span><span class='op'>-</span><span class='nam'>block</span><span class='op'>.</span><span class='nam'>starting_address</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>size</span> <span class='op'>=</span> <span class='num'>1</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='op'>(</span><span class='nam'>type</span><span class='op'>(</span><span class='nam'>values</span><span class='op'>)</span> <span class='key'>is</span> <span class='nam'>list</span><span class='op'>)</span> <span class='key'>or</span> <span class='op'>(</span><span class='nam'>type</span><span class='op'>(</span><span class='nam'>values</span><span class='op'>)</span> <span class='key'>is</span> <span class='nam'>tuple</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>size</span> <span class='op'>=</span> <span class='nam'>len</span><span class='op'>(</span><span class='nam'>values</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='op'>(</span><span class='nam'>offset</span> <span class='op'>&lt;</span> <span class='num'>0</span><span class='op'>)</span> <span class='key'>or</span> <span class='op'>(</span><span class='op'>(</span><span class='nam'>offset</span> <span class='op'>+</span> <span class='nam'>size</span><span class='op'>)</span> <span class='op'>&gt;</span> <span class='nam'>block</span><span class='op'>.</span><span class='nam'>size</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>681</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>OutOfModbusBlockError</span><span class='op'>,</span> <span class='str'>&quot;address %s size %d is out of block %s&quot;</span> <span class='op'>%</span> <span class='op'>(</span><span class='nam'>address</span><span class='op'>,</span> <span class='nam'>size</span><span class='op'>,</span> <span class='nam'>block_name</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'>#if Ok: write the values</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='op'>(</span><span class='nam'>type</span><span class='op'>(</span><span class='nam'>values</span><span class='op'>)</span> <span class='key'>is</span> <span class='nam'>list</span><span class='op'>)</span> <span class='key'>or</span> <span class='op'>(</span><span class='nam'>type</span><span class='op'>(</span><span class='nam'>values</span><span class='op'>)</span> <span class='key'>is</span> <span class='nam'>tuple</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>block</span><span class='op'>.</span><span class='nam'>data</span><span class='op'>[</span><span class='nam'>offset</span><span class='op'>:</span><span class='nam'>offset</span><span class='op'>+</span><span class='nam'>len</span><span class='op'>(</span><span class='nam'>values</span><span class='op'>)</span><span class='op'>]</span> <span class='op'>=</span> <span class='nam'>values</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>else</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>block</span><span class='op'>.</span><span class='nam'>data</span><span class='op'>[</span><span class='nam'>offset</span><span class='op'>]</span> <span class='op'>=</span> <span class='nam'>values</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>get_values</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>block_name</span><span class='op'>,</span> <span class='nam'>address</span><span class='op'>,</span> <span class='nam'>size</span><span class='op'>=</span><span class='num'>1</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; return the values of n items at the given address of the given block</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>with</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_data_lock</span><span class='op'>:</span> <span class='com'>#thread safe</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>block</span> <span class='op'>=</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_get_block</span><span class='op'>(</span><span class='nam'>block_name</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># the block has been found</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'># check that it doesn&#39;t write out of the block</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>offset</span> <span class='op'>=</span> <span class='nam'>address</span> <span class='op'>-</span> <span class='nam'>block</span><span class='op'>.</span><span class='nam'>starting_address</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='op'>(</span><span class='nam'>offset</span><span class='op'>&lt;</span><span class='num'>0</span><span class='op'>)</span> <span class='key'>or</span> <span class='op'>(</span><span class='op'>(</span><span class='nam'>offset</span> <span class='op'>+</span> <span class='nam'>size</span><span class='op'>)</span> <span class='op'>&gt;</span> <span class='nam'>block</span><span class='op'>.</span><span class='nam'>size</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>701</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>OutOfModbusBlockError</span><span class='op'>,</span> <span class='str'>&quot;address %s size %d is out of block %s&quot;</span> <span class='op'>%</span> <span class='op'>(</span><span class='nam'>address</span><span class='op'>,</span> <span class='nam'>size</span><span class='op'>,</span> <span class='nam'>block_name</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'>#returns the values</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>size</span> <span class='op'>==</span> <span class='num'>1</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='op'>(</span><span class='nam'>block</span><span class='op'>.</span><span class='nam'>data</span><span class='op'>[</span><span class='nam'>offset</span><span class='op'>]</span><span class='op'>,</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>else</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>tuple</span><span class='op'>(</span><span class='nam'>block</span><span class='op'>.</span><span class='nam'>data</span><span class='op'>[</span><span class='nam'>offset</span><span class='op'>:</span><span class='nam'>offset</span><span class='op'>+</span><span class='nam'>size</span><span class='op'>]</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='key'>class</span> <span class='nam'>Databank</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;A databank is a shared place containing the data of all slaves&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>__init__</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Constructor&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_slaves</span> <span class='op'>=</span> <span class='op'>{</span><span class='op'>}</span> <span class='com'># the map of slaves by ids</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_lock</span> <span class='op'>=</span> <span class='nam'>threading</span><span class='op'>.</span><span class='nam'>Lock</span><span class='op'>(</span><span class='op'>)</span> <span class='com'># protect access to the map of slaves</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>add_slave</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>slave_id</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Add a new slave with the given id&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>with</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_lock</span><span class='op'>:</span> <span class='com'>#thread-safe</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='op'>(</span><span class='nam'>slave_id</span> <span class='op'>&lt;=</span> <span class='num'>0</span><span class='op'>)</span> <span class='key'>or</span> <span class='op'>(</span><span class='nam'>slave_id</span> <span class='op'>&gt;</span> <span class='num'>247</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>721</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>Exception</span><span class='op'>,</span> <span class='str'>&quot;Invalid slave id %d&quot;</span> <span class='op'>%</span> <span class='op'>(</span><span class='nam'>slave_id</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='key'>not</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_slaves</span><span class='op'>.</span><span class='nam'>has_key</span><span class='op'>(</span><span class='nam'>slave_id</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>726</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_slaves</span><span class='op'>[</span><span class='nam'>slave_id</span><span class='op'>]</span> <span class='op'>=</span> <span class='nam'>Slave</span><span class='op'>(</span><span class='nam'>slave_id</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_slaves</span><span class='op'>[</span><span class='nam'>slave_id</span><span class='op'>]</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>else</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>DuplicatedKeyError</span><span class='op'>,</span> <span class='str'>&quot;Slave %d already exists&quot;</span> <span class='op'>%</span> <span class='op'>(</span><span class='nam'>slave_id</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>get_slave</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>slave_id</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Get the slave with the given id&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>with</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_lock</span><span class='op'>:</span> <span class='com'>#thread-safe</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_slaves</span><span class='op'>.</span><span class='nam'>has_key</span><span class='op'>(</span><span class='nam'>slave_id</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>734</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_slaves</span><span class='op'>[</span><span class='nam'>slave_id</span><span class='op'>]</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>else</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>MissingKeyError</span><span class='op'>,</span> <span class='str'>&quot;Slave %d doesn&#39;t exist&quot;</span> <span class='op'>%</span> <span class='op'>(</span><span class='nam'>slave_id</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>remove_slave</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>slave_id</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Remove the slave with the given id&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>with</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_lock</span><span class='op'>:</span> <span class='com'>#thread-safe</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_slaves</span><span class='op'>.</span><span class='nam'>has_key</span><span class='op'>(</span><span class='nam'>slave_id</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_slaves</span><span class='op'>.</span><span class='nam'>pop</span><span class='op'>(</span><span class='nam'>slave_id</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>else</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>MissingKeyError</span><span class='op'>,</span> <span class='str'>&quot;Slave %d already exists&quot;</span> <span class='op'>%</span> <span class='op'>(</span><span class='nam'>slave_id</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>remove_all_slaves</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;clean the list of slaves&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>with</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_lock</span><span class='op'>:</span> <span class='com'>#thread-safe</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_slaves</span><span class='op'>.</span><span class='nam'>clear</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>handle_request</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>query</span><span class='op'>,</span> <span class='nam'>request</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; when a request is received, handle it and returns the response pdu</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>request_pdu</span> <span class='op'>=</span> <span class='str'>&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>try</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'>#extract the pdu and the slave id</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='op'>(</span><span class='nam'>slave_id</span><span class='op'>,</span> <span class='nam'>request_pdu</span><span class='op'>)</span> <span class='op'>=</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>parse_request</span><span class='op'>(</span><span class='nam'>request</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'>#get the slave and let him executes the action</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>slave_id</span> <span class='op'>==</span> <span class='num'>0</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'>#broadcast</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>key</span> <span class='key'>in</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_slaves</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_slaves</span><span class='op'>[</span><span class='nam'>key</span><span class='op'>]</span><span class='op'>.</span><span class='nam'>handle_request</span><span class='op'>(</span><span class='nam'>request_pdu</span><span class='op'>,</span> <span class='nam'>broadcast</span><span class='op'>=</span><span class='nam'>True</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>else</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>slave</span> <span class='op'>=</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>get_slave</span><span class='op'>(</span><span class='nam'>slave_id</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>response_pdu</span> <span class='op'>=</span> <span class='nam'>slave</span><span class='op'>.</span><span class='nam'>handle_request</span><span class='op'>(</span><span class='nam'>request_pdu</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='com'>#make the full response</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>response</span> <span class='op'>=</span> <span class='nam'>query</span><span class='op'>.</span><span class='nam'>build_response</span><span class='op'>(</span><span class='nam'>response_pdu</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>response</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>except</span> <span class='nam'>Exception</span><span class='op'>,</span> <span class='nam'>excpt</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>call_hooks</span><span class='op'>(</span><span class='str'>&quot;modbus.Databank.on_error&quot;</span><span class='op'>,</span> <span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>excpt</span><span class='op'>,</span> <span class='nam'>request_pdu</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>LOGGER</span><span class='op'>.</span><span class='nam'>error</span><span class='op'>(</span><span class='str'>&quot;handle request failed: &quot;</span> <span class='op'>+</span> <span class='nam'>str</span><span class='op'>(</span><span class='nam'>excpt</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>except</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>LOGGER</span><span class='op'>.</span><span class='nam'>error</span><span class='op'>(</span><span class='str'>&quot;handle request failed: unknown error&quot;</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='com'>#If the request was not handled correctly, return a server error response</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>func_code</span> <span class='op'>=</span> <span class='num'>1</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>len</span><span class='op'>(</span><span class='nam'>request_pdu</span><span class='op'>)</span> <span class='op'>&gt;</span> <span class='num'>0</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='op'>(</span><span class='nam'>func_code</span><span class='op'>,</span> <span class='op'>)</span> <span class='op'>=</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>unpack</span><span class='op'>(</span><span class='str'>&quot;&gt;B&quot;</span><span class='op'>,</span> <span class='nam'>request_pdu</span><span class='op'>[</span><span class='num'>0</span><span class='op'>]</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>struct</span><span class='op'>.</span><span class='nam'>pack</span><span class='op'>(</span><span class='str'>&quot;&gt;BB&quot;</span><span class='op'>,</span> <span class='nam'>func_code</span><span class='op'>+</span><span class='num'>0x80</span><span class='op'>,</span> <span class='nam'>defines</span><span class='op'>.</span><span class='nam'>SLAVE_DEVICE_FAILURE</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='com'>#-------------------------------------------------------------------------------</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='key'>class</span> <span class='nam'>Server</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; This class owns several slaves and defines an interface</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; to be implemented for a TCP or RTU server</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>__init__</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>databank</span><span class='op'>=</span><span class='nam'>None</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Constructor&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_databank</span> <span class='op'>=</span> <span class='nam'>databank</span> <span class='key'>if</span> <span class='nam'>databank</span> <span class='key'>else</span> <span class='nam'>Databank</span><span class='op'>(</span><span class='op'>)</span> <span class='com'>#never use a mutable type as default argument</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_verbose</span> <span class='op'>=</span> <span class='nam'>False</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_thread</span> <span class='op'>=</span> <span class='nam'>None</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_go</span> <span class='op'>=</span> <span class='nam'>None</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_make_thread</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_do_init</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;executed before the server starts: to be overridden&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>pass</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_do_exit</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;executed after the server stops: to be overridden&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>pass</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_do_run</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;main function of the server: to be overridden&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>pass</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_make_thread</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;create the main thread of the server&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_thread</span> <span class='op'>=</span> <span class='nam'>threading</span><span class='op'>.</span><span class='nam'>Thread</span><span class='op'>(</span><span class='nam'>target</span><span class='op'>=</span><span class='nam'>Server</span><span class='op'>.</span><span class='nam'>_run_server</span><span class='op'>,</span> <span class='nam'>args</span><span class='op'>=</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_go</span> <span class='op'>=</span> <span class='nam'>threading</span><span class='op'>.</span><span class='nam'>Event</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>set_verbose</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>verbose</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;if verbose is true the sent and received packets will be logged&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_verbose</span> <span class='op'>=</span> <span class='nam'>verbose</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>get_db</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;returns the databank&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_databank</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>add_slave</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>slave_id</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;add slave to the server&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_databank</span><span class='op'>.</span><span class='nam'>add_slave</span><span class='op'>(</span><span class='nam'>slave_id</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>get_slave</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>slave_id</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;get the slave with the given id&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_databank</span><span class='op'>.</span><span class='nam'>get_slave</span><span class='op'>(</span><span class='nam'>slave_id</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>remove_slave</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>slave_id</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;remove the slave with the given id&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_databank</span><span class='op'>.</span><span class='nam'>remove_slave</span><span class='op'>(</span><span class='nam'>slave_id</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>remove_all_slaves</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;remove the slave with the given id&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_databank</span><span class='op'>.</span><span class='nam'>remove_all_slaves</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_make_query</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; Returns an instance of a Query subclass implementing </span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; the MAC layer protocol</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>NotImplementedError</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>start</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Start the server. It will handle request&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_go</span><span class='op'>.</span><span class='nam'>set</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_thread</span><span class='op'>.</span><span class='nam'>start</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>stop</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;stop the server. It doesn&#39;t handle request anymore&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_thread</span><span class='op'>.</span><span class='nam'>isAlive</span><span class='op'>(</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_go</span><span class='op'>.</span><span class='nam'>clear</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_thread</span><span class='op'>.</span><span class='nam'>join</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_run_server</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;main function of the main thread&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>try</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_do_init</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>while</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_go</span><span class='op'>.</span><span class='nam'>isSet</span><span class='op'>(</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_do_run</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>LOGGER</span><span class='op'>.</span><span class='nam'>info</span><span class='op'>(</span><span class='str'>&quot;%s has stopped&quot;</span> <span class='op'>%</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>__class__</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_do_exit</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>except</span> <span class='nam'>Exception</span><span class='op'>,</span> <span class='nam'>excpt</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>LOGGER</span><span class='op'>.</span><span class='nam'>error</span><span class='op'>(</span><span class='str'>&quot;server error: %s&quot;</span> <span class='op'>%</span> <span class='nam'>str</span><span class='op'>(</span><span class='nam'>excpt</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_handle</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>request</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;handle a received sentence&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_verbose</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>871</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>LOGGER</span><span class='op'>.</span><span class='nam'>debug</span><span class='op'>(</span><span class='nam'>utils</span><span class='op'>.</span><span class='nam'>get_log_buffer</span><span class='op'>(</span><span class='str'>&quot;--&gt;&quot;</span><span class='op'>,</span> <span class='nam'>request</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='com'>#gets a query for analyzing the request</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>query</span> <span class='op'>=</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_make_query</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>retval</span> <span class='op'>=</span> <span class='nam'>call_hooks</span><span class='op'>(</span><span class='str'>&quot;modbus.Server.before_handle_request&quot;</span><span class='op'>,</span> <span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>request</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>retval</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>878</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>request</span> <span class='op'>=</span> <span class='nam'>retval</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>response</span> <span class='op'>=</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_databank</span><span class='op'>.</span><span class='nam'>handle_request</span><span class='op'>(</span><span class='nam'>query</span><span class='op'>,</span> <span class='nam'>request</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>retval</span> <span class='op'>=</span> <span class='nam'>call_hooks</span><span class='op'>(</span><span class='str'>&quot;modbus.Server.after_handle_request&quot;</span><span class='op'>,</span> <span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>response</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>retval</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>883</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>response</span> <span class='op'>=</span> <span class='nam'>retval</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>response</span> <span class='key'>and</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_verbose</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>886</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>LOGGER</span><span class='op'>.</span><span class='nam'>debug</span><span class='op'>(</span><span class='nam'>utils</span><span class='op'>.</span><span class='nam'>get_log_buffer</span><span class='op'>(</span><span class='str'>&quot;&lt;--&quot;</span><span class='op'>,</span> <span class='nam'>response</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>response</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
    
</td>
</tr>
</table>
</div>

</body>
</html>
